X-Authentication-Warning: delorie.com: mailnull set sender to djgpp-bounces using -f From: "Tim Van Holder" Subject: Re: BUG? : Constructing member objects on-the-fly inside constructors. Date: Mon, 14 Jan 2002 08:25:09 +0100 Newsgroups: comp.os.msdos.djgpp Organization: Anubex N.V. Message-ID: References: User-Agent: Pan/0.10.0 (Unix) Mime-Version: 1.0 Content-Type: text/plain; charset=ANSI_X3.4-1968 Content-Transfer-Encoding: 8bit X-Comment-To: "Robert Hardy" X-No-Productlinks: Yes Lines: 80 NNTP-Posting-Host: 194.78.64.238 X-Trace: 1010993323 reader0.news.skynet.be 75157 194.78.64.238 X-Complaints-To: abuse AT skynet DOT be To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com > class contains_int { > int i; > public: > contains_int(int ii): i(ii) {} > void printme() {cout << "( int: " << i << " )"; } int > getme() {return i;} > }; > > class contains_contains_int { > contains_int c_i; > public: > contains_contains_int(contains_int c_ii): c_i(c_ii) {} void > printme() {cout << "( c_i : "; > c_i.printme(); cout << " )"; } > }; > > class contains_contains_contains_int { > contains_contains_int cc_i; > public: > contains_contains_contains_int(contains_contains_int cc_ii) > : cc_i(cc_ii) {} > void printme() {cout << "( cc_i : "; > cc_i.printme(); cout << " )"; } > }; Just one question: why not have them all take an int as constructor argument and pass that down? class cont_int { int _i; public: cont_int(int i) : _i(i) {} ... }; class cont_cont_int { cont_int _ci; public: cont_cont_int(int i) : _ci(i) {} ... }; class cont_cont_cont_int { int _cci; public: cont_cont_cont_int(int i) : _cci(i) {} ... }; Of course, you can also define the constructors that take the actual objects, depending on whether they're needed (and even then, I'd be inclined to use their get() accessors). This may also solve the problem below. > contains_contains_contains_int x( contains_contains_int ( > contains_int(10) )); cout << " x = "; x.printme(); cout << endl; > //line A > > } > > In function `int main()': > Error (line A): request for member `printme' in `x', which is of > non-aggregate type `contains_contains_contains_int ()()' My guess is that the compile chose to interpret the declaration of x as a function declaration (it's a known ambiguity in the language, though I thought it was limited to templatized types). Writing it as type name = type(args); instead of type name(args); would probably help, as would using a simpler constructor (such as passing down the ints, as they are really what matters; how you contain it is an implementation detail). HTH