Xref: utzoo comp.lang.c++:13517 comp.std.c++:923 Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!cimshop!davidm From: cimshop!davidm@uunet.UU.NET (David S. Masterson) Newsgroups: comp.lang.c++,comp.std.c++ Subject: Re: Constructor Limitation: [Was: Re: Constructor question] Message-ID: Date: 17 May 91 17:41:59 GMT References: <1991Apr2.110623.22219@and.cs.liv.ac.uk> <20164@alice.att.com> <1991Apr18.005426.21863@dragon.wpd.sgi.com> <71952@microsoft.UUCP> Sender: davidm@cimshop.UUCP Distribution: comp Organization: Consilium Inc., Mountain View, California Lines: 55 In-reply-to: dsouza@gwen.cad.mcc.com's message of 3 May 91 20:03:07 GMT >>>>> On 3 May 91 20:03:07 GMT, dsouza@gwen.cad.mcc.com (Desmond Dsouza) said: Desmond> In article <71952@microsoft.UUCP> jimad@microsoft.UUCP (Jim ADCOCK) Desmond> writes: Jim> Again, if you want to factor out common code from constructors, I Jim> recommend you follow Bjarne's suggestion: write a private init() routine Jim> which is called from within each constructor. Desmond> Sorry, but I dont think this works in general. Desmond> All data members which are objects have to be initialized before Desmond> the body of the constructor. There is this basic problem with Desmond> computing arguments for constructors in the initialization list: Desmond> You cannot compute any temporaries (e.g. common subexpressions) which Desmond> are needed for initialization of such data members. Desmond> class A { public: A(int m, int n); }; Desmond> class B { Desmond> public: Desmond> B (int j); Desmond> private: Desmond> A a1; // initialized with F1(j), F1(j)+1 for Desmond> //some horribly complex F1 Desmond> }; Desmond> B::B(int j) Desmond> // how can I share the computation of F1(j) here ??? Desmond> : a1 (F1(j), F1(j)+1) Desmond> { Desmond> // this part does not help: I cannot modify a1 at this point. Desmond> } WARNING: Novice code coming... Would a possible solution to this be: B::B(int j) : a1() { int x = F1(j), y = x + 1; A temp(x,y); a1 = temp; // why can't you modify a1 using copy assignment? } -- ==================================================================== David Masterson Consilium, Inc. (415) 691-6311 640 Clyde Ct. uunet!cimshop!davidm Mtn. View, CA 94043 ==================================================================== "If someone thinks they know what I said, then I didn't say it!"