In Java, we can write as following:
public class Demo{
private int val;
public Demo(int val){this.val = val;}
public Demo(){this(0);}
}
However, it does not work in C++:
class Demo{
private:
int _val;
public:
Demo(int val):_val(val){}
Demo(){this(0);}
}
How to revise this code?
With C++11, you could use delegate constructor:
Demo():Demo(0){ }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With