Is it possible (C++11) to call another constructor from the list of initializers of a delegating constructor? (I've tried, and it seems that we can't!)
Something like this:
1 2 3 4 5 6 7
class MyClass
{
int x=0, y=0, z=0;//finally, they let us initialize the variables here!
public:
MyClass(int _x, int _y) : x(_x), y(_y), MyClass(0){}
MyClass(int _z) : z(_z){}
};
You can't initialize members in the initializer list of a delegating constructor. Just think what would happen in your code if x was a string. First it would be constructed by the delegated constructor and then it would be constructed once again in the delegating constructor (with a different constructor).
You can delegate work of initialization to another constuctor. So you cannot do anything else in initialization lists.
However there is workarounds. For example:
1 2 3 4 5 6 7 8 9
class MyClass
{
int x=0, y=0, z=0;
public:
MyClass(int _x, int _y) : MyClass(x, y, 0){}
MyClass(int _z) :MyClass(0, 0, z){}
private:
MyClass(int _x, int _y, int _z) : x(_x), y(_y), z(_z) {}
};