Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!cs.utexas.edu!uunet!godzilla!nujoizey!gwu From: gwu@nujoizey.tcs.com (George Wu) Newsgroups: comp.lang.c++ Subject: Re: Constructor question Keywords: common constructor code Message-ID: <1988@godzilla.tcs.com> Date: 3 Apr 91 21:26:43 GMT References: <1991Apr2.110623.22219@and.cs.liv.ac.uk> Sender: root@tcs.com Reply-To: gwu@nujoizey.tcs.com (George Wu) Organization: Teknekron Communications Systems Lines: 37 Cc: markr@and.cs.liv.ac.uk In article <1991Apr2.110623.22219@and.cs.liv.ac.uk>, 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 What you wish to do is commonly accomplished by calling a third method containing the common initlization code. For example: X::init() { // initialization of code common to all constructors goes here } X::X(int i) { init(); // . . . } X::X(char *s) { init(); // . . . } ---- George J Wu, Software Engineer | gwu@tcs.com or uunet!tcs!gwu Teknekron Communications Systems, Inc.| (415) 649-3752 2121 Allston Way, Berkeley, CA, 94704 | Quit reading news. Get back to work.