delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/29/06:51:52

Date: Thu, 29 Jan 1998 04:19:23 -0500 (EST)
Message-Id: <199801290919.EAA18880@p2.acadia.net>
To: "Chia" <chia AT top DOT net>
Subject: Re: Easy C++ stuff... I hope, anyway.
Cc: djgpp AT delorie DOT com
References: <6aolmn$3vs AT nnrp3 DOT farm DOT idt DOT net>
in-reply-to: <6aolmn$3vs@nnrp3.farm.idt.net>
From: swarnerx3 AT acadia DOT net (Scott Warner)
MIME-Version: 1.0


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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019