Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling a constructor from another constructor

Status
Not open for further replies.

maluk

Programmer
Oct 12, 2002
79
SG
Will it be possible to call a contructor from another constructor in a single class?

Rome did not create a great empire by having meetings, they did it by
killing all those who opposed them.

- janvier -
 
No, it's not possible. Make private member(s) with common constructing functionality then call it(s) in constructors...
 
Hello all,

it IS quite possible to do just that:

Code:
class B {
   public:
   B(int i) { m_I = i; }
   private:
   int m_I;
};

class A {
   public:
   A() : m_B( 0 ) // <- constructor call for aggregate 
                  //     object
   {}
   protected:
   B m_B;
};

This is called "constructor initializer list". Google for details.

Cheers,
Tobi
 
LF28,
it's a trivial (and canonical;) using of parent class(e)constructors. No need to search Google for it: it's in any C++ book.
Original post context was (obviously;) explicit call[\i] (as in PHP, for example)...
 
oh how could I miss this? Now that I know the the secrets of constructor lists really are already published in print, I do not deem myself a genius any longer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top