Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 calling constructor from constructor of same class type

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?

like image 696
user974967 Avatar asked May 03 '12 00:05

user974967


People also ask

Can a constructor call another constructor of the same class?

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().

How can we call one constructor from another in same class?

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.

Can we call constructor within constructor?

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.

Can copy constructor call another constructor?

In C++03, you can't call one constructor from another (called a delegating constructor).


1 Answers

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.

like image 102
David Avatar answered Sep 17 '22 23:09

David