Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!apple!netcom!sjsumcs!horstman From: horstman@mathcs.sjsu.edu (Cay Horstmann) Newsgroups: comp.lang.c++ Subject: Re: Constructor question Message-ID: <1991Apr5.035053.27973@mathcs.sjsu.edu> Date: 5 Apr 91 03:50:53 GMT References: <1991Apr2.110623.22219@and.cs.liv.ac.uk> Organization: San Jose State University - Math/CS Dept. Lines: 39 In article fuchs@t500e0.telematik.informatik.uni-karlsruhe.de (Harald Fuchs) writes: >markr@and.cs.liv.ac.uk writes: > >>Can I call one constructor to class X explicitly from within another >>constructor to class X ? > >>Example: constructor X::X( int i ) performs a lot of initialisation that needs >>to be done at the start of X::X( char *c ) too. Can I write: > >>X::X( char *c ) { >> X( 5 ); >> // rest of stuff > > >>? Or maybe something similar with different syntax? > >>Apologies for turning the group into a tutorial session, but I can't find any >>reference to this in my C++ books. > >That's because there is no such thing. That isn't quite true. One could presumably call the constructor explicitly with the placement syntax: new(this) X(5); I am NOT going to advocate this style. In fact, it seems the totally WRONG thing to do since it can be brokem by redefining X::operator new( size_t, void* ). >When I have to do lots of >common initialization, I put it into a private member function >(e.g. void init ();) and call this function from the constructors. >Unfortunately, this won't work if you have to initialize constant or >reference data members. IMHO a hole in the language. > Good point! Cay