C++ – How to Call One Constructor from Another

c++constructor

class A{
  A(int a = 5){
    DoSomething();
    A();
  }
  A(){...}
}

Can the first constructor call the second one?

Best Answer

Not before C++11.

Extract the common functionality into a separate function instead. I usually name this function construct().

The "so-called" second call would compile, but has a different meaning in C++: it would construct a new object, a temporary, which will then be instantly deleted at the end of the statement. So, no.

A destructor, however, can be called without a problem.

Related Question