Mail Archives: djgpp/1998/01/29/06:51:52
In message <6aolmn$3vs AT nnrp3 DOT farm DOT idt DOT net>, "Chia" <chia AT top DOT net> wrote:
> I've got two classes... Both of them have lots of constructive arguments,
> and I want to have one of them be contained from the other one. Here's what
> I mean:
>
> class a {
> public:
> a(int b) {
> x = b;
> }
> protected:
> int x;
> }
>
> class b {
> public:
> b(int x) {
> // normally, I would just do this:
> // a class_a_instance(x);
> // but that doesn't seem to work.
> // any ideas on how to call class a's constructor
> // if it's part of another class?
> // thanks! chia AT top DOT net
> }
> protected:
> a class_a_instance;
> }
>
Something like
class a
{
public:
a(int b) { x=b; }
private:
int b;
};
class b
{
public:
b(int x) { pa = new a(x); }
private:
a* pa;
};
is an example of containment where your data member class in b (pa) is a pointer that is defined in the constructor of b... you could also try multiple inheritance, but this is a completely different route that I wouldn't recommend if containment is possible.
- Raw text -