Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Combining constructors

22 views
Skip to first unread message

Paul

unread,
Sep 28, 2018, 5:00:05 PM9/28/18
to
I have a class A which implements an instantiation of class B with
some default parameters.
class B in turn depends on other classes.

So my design looks like this.

class B
{
public:
B(const C&);

protected:
void implementInBaseClass();
};

class A : public B
{
public:
A();

void someFunction();
};

A::A() : B(C(D(OtherParams)))
{}

void A::someFunction()
{
implementInBaseClass;
}

This code works fine, but I lack the vocab to explain the design.
It looks similar to the strategy design pattern.

In particular I don't know the term for combining constructors

as in B(C(D(E(F(params)))))... where B, C, D, E, F are classes.

If you think this idea looks bad and have other ideas, that is welcome
too.

Thank you,

Paul

Paul

unread,
Sep 28, 2018, 5:03:41 PM9/28/18
to
Sorry but it's difficult to avoid typos when I'm not using a compiler.
I meant void A::someFunction()
{
implementInBaseClass();
}

Öö Tiib

unread,
Sep 28, 2018, 6:00:38 PM9/28/18
to
May be I am wrong but such "A(B(C(D())))" feel like attempt of
art ... designing and drawing various ornaments. Is it useful for
something unknowable, or maybe it is not I can't know. It is sure
that it is not an algorithm.

Solving problems does not work like that. We do not throw up code
patterns to see if it maybe helps with something but instead we take
real problem and try to invent an algorithm to solve it. Different
algorithms solve different problems.

So, take actual practical goal to solve. To solve it write algorithm
that has input (may be just constant test input) data and output
(may be just assert that output of algorithm is correct). In
context of such algorithms also patterns start to make sense. ;)





Paul

unread,
Sep 28, 2018, 6:44:59 PM9/28/18
to
implementInBaseClass();
}

The above is certainly not an algorithm but nobody ever said it was.
It's also not a chocolate biscuit.

It is a design pattern, though.

So I was asking the name of the design pattern.

Paul

Öö Tiib

unread,
Sep 28, 2018, 7:33:14 PM9/28/18
to
I try to express it then through analogy. Room design patterns are meant
for purpose, for certain activities in those rooms, you know bathroom,
toilet, kitchen, bedroom, living room, studio, garage &c.
Similarly software module design patterns are meant for certain purpose
for running certain algorithms in those modules. Creational design
patterns are for constructing certain types of data, structural design
patterns are for composing relations between objects to acquire
certain functionality and behavioral patterns are also for purpose
usually about communication between objects. So all of it is
about purposeful activities. If there are no purpose then there are
no pattern.

Paul

unread,
Sep 28, 2018, 8:02:16 PM9/28/18
to
The problem is that you have a class B and you want to encapsulate the
idea of "a particular instantiation of class B"
So one idea is to use another class to encapsulate the idea of
"a particular instantiation of class B"

So I use class A for that purpose.

I'll try to be a bit more concrete --

int f(double w, double x, double y, double z)
is an intricate mathematical function

B is a class for calculating f.

The problem is to illustrate this procedure for the numbers
1.1, 2.3, 4.5, 5.6 -- call the corresponding vector vec.

So I write

class B
{
public:
B(const std::vector<double>&);

protected:
int f(); // Computes f with respect to 4-dim vector in constructor
};

class A : public B
{

public:
A();

int F();
}

A::A() : B(vec)
{

}

int A::F()
{
return f();
}

int main()
{
A a;
std::cout << a.F() << " is the value of f when applied to the
illustrative parameters in vec ";
}



Öö Tiib

unread,
Sep 28, 2018, 8:40:45 PM9/28/18
to
Whole class for very concrete instantiation? That is done usually only
in unit tests. So pattern is "mock wrapper for unit testing" or
something like that.

Jorgen Grahn

unread,
Sep 29, 2018, 2:47:02 AM9/29/18
to
On Fri, 2018-09-28, Paul wrote:
> I have a class A which implements an instantiation of class B with
> some default parameters.

I had trouble reading that sentence. How is it different from
"A specializes B, by simplifying its construction"?

Also, would a plain function work for you?

B A() { return B(C(D(OtherParams))); }

...

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Paul

unread,
Sep 29, 2018, 5:15:45 AM9/29/18
to
On Saturday, September 29, 2018 at 7:47:02 AM UTC+1, Jorgen Grahn wrote:
> On Fri, 2018-09-28, Paul wrote:
> > I have a class A which implements an instantiation of class B with
> > some default parameters.
>
> I had trouble reading that sentence. How is it different from
> "A specializes B, by simplifying its construction"?
>
> Also, would a plain function work for you?
>
> B A() { return B(C(D(OtherParams))); }
>
> ...
>

I should have said "A specializes B by simplifying its construction."
That's better. Thanks.

And thanks to Oo Tiib too

And thanks to everyone else.

Paul
0 new messages