Newsgroups: comp.os.msdos.djgpp From: "A. Jans-Beken" Subject: Re: Easy C++ stuff... I hope, anyway. Content-Type: text/plain; charset=us-ascii Message-ID: <34D1EC54.5105@oce.nl> Sender: news AT oce DOT nl (The Daily News @ nntp01.oce.nl) Content-Transfer-Encoding: 7bit Organization: Océ-Nederland B.V. References: <6aolmn$3vs AT nnrp3 DOT farm DOT idt DOT net> Mime-Version: 1.0 Date: Fri, 30 Jan 1998 15:05:56 GMT Lines: 42 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk Chia 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); This IMO is no good C++ code ^^^^ You can use: a class_a_instance but not a class_a_instance(x) Why? During the definition of class B you still have NOT an instance of B. Therefore you can not initialize a member of B. You have to initialize A in your constructor for B. The constructor for B should be: b::b(int x): a(x) { /* init other members of B here */ } > // 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; > }