Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a colon following a C++ constructor name do? [duplicate]

What does the colon operator (":") do in this constructor? Is it equivalent to MyClass(m_classID = -1, m_userdata = 0);?

class MyClass { public:      MyClass() : m_classID(-1), m_userdata(0) {      }      int m_classID;     void *m_userdata; }; 
like image 968
spencewah Avatar asked Aug 13 '09 15:08

spencewah


People also ask

What does colon after constructor mean?

It means that len is not set using the default constructor.

What is the use of colon in C++?

In C++, the colon ":" operator, to my understanding, is used for inheritance.

What are constructors in C++?

A constructor is a special type of member function that is called automatically when an object is created. In C++, a constructor has the same name as that of the class and it does not have a return type. For example, class Wall { public: // create a constructor Wall() { // code } };


2 Answers

This is an initialization list, and is part of the constructor's implementation.

The constructor's signature is:

MyClass(); 

This means that the constructor can be called with no parameters. This makes it a default constructor, i.e., one which will be called by default when you write MyClass someObject;.

The part : m_classID(-1), m_userdata(0) is called initialization list. It is a way to initialize some fields of your object (all of them, if you want) with values of your choice, instead of leaving them as undefined.

After executing the initialization list, the constructor body (which happens to be empty in your example) is executed. Inside it you could do more assignments, but once you have entered it all the fields have already been initialized - either to random, unspecified values, or to the ones you chose in your initialization list. This means the assignments you do in the constructor body will not be initializations, but changes of values.

like image 64
Daniel Daranas Avatar answered Sep 25 '22 21:09

Daniel Daranas


It is an initialization list.

By the time you get in the body of the constructor, all fields have already been constructed; if they have default constructors, those were already called. Now, if you assign a value to them in the body of the constructor, you are calling the copy assignment operator, which may mean releasing and reacquiring resources (e.g. memory) if the object has any.

So in the case of primitive types like int, there's no advantage compared to assigning them in the body of the constructor. In the case of objects that have a constructor, it is a performance optimization because it avoids going through two object initializations instead of one.

An initialization list is necessary if one of the fields is a reference because a reference can never be null, not even in the brief time between object construction and the body of the constructor. The following raises error C2758: 'MyClass::member_' : must be initialized in constructor base/member initializer list

class MyClass { public :     MyClass(std::string& arg) {         member_ = arg;     }     std::string& member_; }; 

The only correct way is:

class MyClass { public :     MyClass(std::string& arg)          : member_(arg)      {     }     std::string& member_; }; 
like image 24
Asik Avatar answered Sep 23 '22 21:09

Asik