Xref: utzoo comp.lang.c++:13556 comp.std.c++:924 Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!cs.utexas.edu!radar!cadillac!dsouza From: dsouza@gwen.cad.mcc.com (Desmond Dsouza) Newsgroups: comp.lang.c++,comp.std.c++ Subject: Re: Constructor Limitation: [Was: Re: Constructor question] Message-ID: Date: 20 May 91 20:44:06 GMT References: <1991Apr2.110623.22219@and.cs.liv.ac.uk> <20164@alice.att.com> <1991Apr18.005426.21863@dragon.wpd.sgi.com> <71952@microsoft.UUCP> 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? } -- You make assumptions about default constructor (0 arguments) and the assignment operator which are not valid in general. Someone else suggested deriving a class from A and using that instead. That is a pretty expensive way of computing a temporary integer! -- Desmond. -- ------------------------------------------------------------------------------- Desmond D'Souza, MCC CAD Program | ARPA: dsouza@mcc.com | Phone: [512] 338-3324 Box 200195, Austin, TX 78720 | UUCP: {uunet,harvard,gatech,pyramid}!cs.utexas.edu!milano!cadillac!dsouza