Xref: utzoo comp.lang.c++:13619 comp.std.c++:930 Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!nstn.ns.ca!news.cs.indiana.edu!samsung!zaphod.mps.ohio-state.edu!ncar!gatech!bloom-beacon!eru!hagbard!sunic!ericom!eua.ericsson.se!euaeny From: euaeny@eua.ericsson.se (Erik Nyquist) Newsgroups: comp.lang.c++,comp.std.c++ Subject: Re: Constructor Limitation: [Was: Re: Constructor question] Message-ID: <1991May23.170530.23037@eua.ericsson.se> Date: 23 May 91 17:05:30 GMT References: <1991Apr2.110623.22219@and.cs.liv.ac.uk> <20164@alice.att.com> <1991Apr18.005426.21863@dragon.wpd.sgi.com> <71952@microsoft.UUCP> <72461@microsoft.UUCP> In article <72461@microsoft.UUCP> jimad@microsoft.UUCP (Jim ADCOCK) writes: > > You cannot compute any temporaries (e.g. common subexpressions) > > which are needed for initialization of such data members. > Consider: > ---- > int F1(int j) { /* something really really big and horrible here */ return j;} > class A { > public: A(int m, int n); > }; > class B { > public: > B (int j); > private: > int f1; > A a1; // initialized with F1(j), F1(j)+1 for some horribly complex F1 > }; > B::B(int j) > // how can I share the computation of F1(j) here ??? !!! Try the following: > : f1(F1(j)), a1(f1, f1+1) > { > // this part does not help: I cannot modify a1 at this point. > } >No good. You cannot afford to add a data member per object instance to >simply store an intermediate constructor variable. >(There is also the subtle point about order of initialization of data > members -- it follows the order in the class definition, so you had it right). Why no extend the language and make it possible to declare temporaries in the initialization list? ;-) (yes, I know! We should try to standardize the language, not change it.) class A { public: A(int m, int n); }; class B { public: B (int j); private: A a1; // initialized with F1(j), F1(j)+1 for some horribly complex F1 }; B::B(int j) // how can I share the computation of F1(j) here ??? !!! Is this what you need? : int f1 = F1(j), a1(f1, f1+1) { // this part does not help: I cannot modify a1 at this point. } I don't say that we should change the language, but how can we solve this problem without language extensions? We could also ask ourselves if we really should try to solve this problem. That surely depends on how common it is! Erik Nyquist Ellemtel Utecklings AB We are no longer the knights that say Ni! Box 1505 We are the knights that say: S-125 25 Alvsjo, Sweden Iky,iky,iky,iky,patang,zoop-boing, zowie. -- Erik Nyquist Ellemtel Utecklings AB We are no longer the knights that say Ni! Box 1505 We are the knights that say: S-125 25 Alvsjo, Sweden Iky,iky,iky,iky,patang,zoop-boing, zowie.