4 answers
In C ++ 03 this cannot be done.
Starting with C ++ 11, you can call one constructor from another, this is called "delegating constructors"
struct X { X(int a, int b) { std::cout << a+b; } X(int b) : X(1, b) {} X() : X(20) {} }; X x; // X::X() вызывает X::X(int), который вызывает X::X(int, int)
At the same time, while calling another constructor, constructors of class members cannot be called, for example:
struct B { B() : x(1) {} B(int x_) : B(), x(x_) {} // ОШИБКА: нельзя одновременно вызвать B() и x() int x; };
As soon as one of the constructors was called, the object is considered fully constructed, so an exception to the calling constructor will lead to the call of the destructor class:
struct C { C(const char*) { throw 1; } C() {} C(int) : C() { throw 1; } ~C() { std::cout << "~C\n"; } }; int main() { try { C c(""); // ничего не будет напечатано, деструктор не будет вызван } catch (...) {} try { C c(1); // будет напечатано "~C" } catch (...) {} }
If it is necessary to duplicate the code in different constructors of the same object, it is better to take it into a separate function that will do all the rough work. And to cause this f-tsiyu from designers.
and what exactly did the author of the question want?
class c1 { public: c1(); c1(int i){printf("\nint ca()");} c1(float i,int c){printf("\nfloat ca()");} }; class test { c1 c; public: test():c(10,4){printf("test\n");} ~test() {printf("\n~test!!!!");} }; int main() { test a; }