From: mpho on
Dear all,

I have this situation.

class X {
int size;
T* val;
.....
public:
X();
X(T *, int);
...
};

Parameterized constructor is X::X(T *p, int s) : size(s), val(new
T[s]), { ... val =p; ... }

class A {
int size;
T * val1;
X x_obj;
.....
public:
A();
A(T *, int );
......
};

Parameterized constructor is: A::A(T *q, int s) : size(s), val1(new
T[s]), x_obj(val1){ ... val1 = q; ... }

class B {
int size;
T * val2;
X x_obj;
.....
public:
B();
B(T *, int s);
.......
};

Parameterized constructor is: B::B(T *r, int s) : size(s), val2(new
T[s]), x_obj(val2){ ... val2 = r; ...}

Every operation not defined in A or in B is serviced by an object of
type X. What they call delegation.

class W {
int size1, size2;
T *val1;
T *val2;
A a_obj;
B b_obj;
......
public:
W();
W(T *, T *, int, int);
.....
};

Every operation not defined in W is serviced by an object of type A or
of type B.

The question is how to invoke W(T *, T *, int s1, int s2)?

Is this the way? :
W::W(T *q, T *r, int s1, int s2) : size1(s1), size2(s2), val1(new
T[s1]), val2(new T[s2]),

a_obj(val1), b_obj(val2) { ... val1 = q; val2 = r; ...}

Given that the contained objects get initialized before the containing
objects, are the signatures of all the container class constructors
above even correct?


To summarize: class W contains classes A and B, which in turn each
contain class X. How do I invoke the constructor for W?

Out of interest, which would be more efficient: composition by value
or composition by pointer or by reference? What happens with the copy
constructor of W and its operator=()?

Thank you all.
Mpho





--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Carl Barron on
mpho <tjabane(a)gmail.com> wrote:

>
> To summarize: class W contains classes A and B, which in turn each
> contain class X. How do I invoke the constructor for W?
>
> Out of interest, which would be more efficient: composition by value
> or composition by pointer or by reference? What happens with the copy
> constructor of W and its operator=()?

I don't see why not simply something like this

struct X
{
std::vector<T> data;
X(){}
X(T *p,int s):data(p,p+s){}
// ...
};

class A:public X
{
A(){}
A(T *p,int s):X(p,s){}
//...
};

class B similiar to A

class W:public A,B
{
W(){}
W(T*p1,T*p2,int s1,int s2):A(p1,s1),B(p2,s2){}
// ...
};


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]