I was told the following was possible due to changes in C++11:
class SomeType {
int number;
public:
SomeType(int new_number) : number(new_number) {}
SomeType() : SomeType(42) {}
};
But when I try to build I get an error:
"SomeType" is not a nonstatic data member or base class of class "SomeType"
error C2614: 'SomeType' : illegal member initialization: 'SomeType' is not a base or member
Is this feature not yet supported in Visual Studio 2010? Do I need to configure something to get this to build? What is wrong?
You can create multiple constructors in the same class, each with a different number of arguments that it accepts. To call one of the constructors in another constructor (of the same class), use the keyword this().
Within same class: It can be done using this() keyword for constructors in the same class. From base class: by using super() keyword to call the constructor from the base class.
The invocation of one constructor from another constructor within the same class or different class is known as constructor chaining in Java. If we have to call a constructor within the same class, we use 'this' keyword and if we want to call it from another class we use the 'super' keyword.
In C++03, you can't call one constructor from another (called a delegating constructor).
It's not supported in VS2010. Most C++11 features are not supported in VS2010 (or VS11 for that matter)
Here is a chart of supported features in VC10 and VC11.
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