4

Possible Duplicate:
What is this weird colon-member syntax in the constructor?

If I define a class as shown below in C++:

class myclass { public: myclass (unsigned int param) : param_ (param) { } unsigned int param () const { return param_; } private: unsigned int param_; }; 

What does the constructor definition: myclass (unsigned int param) : param_ (param) means and the benefit to the code?

0

2 Answers 2

13
myclass (unsigned int param) : param_ (param) 

This construct is called a Member Initializer List in C++.

It initializes your member param_ to a value param.


What is the difference between Initializing And Assignment inside constructor? &
What is the advantage?

There is a difference between Initializing a member using initializer list and assigning it an value inside the constructor body.

When you initialize fields via initializer list the constructors will be called once.

If you use the assignment then the fields will be first initialized with default constructors and then reassigned (via assignment operator) with actual values.

As you see there is an additional overhead of creation & assignment in the latter, which might be considerable for user defined classes.

For an integer data type(for which you use it) or POD class members there is no practical overhead.


When do you HAVE TO use member Initializer list?
You will have(rather forced) to use a Member Initializer list if:

Your class has a reference member
Your class has a const member or
Your class doesn't have a default constructor


A code Example that depict the HAVE TO cases:

class MyClass { public: int &i; //reference member, has to be Initialized in Member Initializer List int j; const int k; //const member, has to be Initialized in Member Initializer List MyClass(int a, int b, int c):i(a),j(b),k(c) { } }; class MyClass2:public MyClass { public: int p; int q; MyClass2(int x,int y,int z,int l,int m):MyClass(x,y,z),p(l),q(m) { } }; int main() { int x = 10; int y = 20; int z = 30; MyClass obj(x,y,z); int l = 40; int m = 50; MyClass2 obj2(x,y,z,l,m); return 0; } 

MyClass2 doesn't have a default constructor so it has to be initialized through member initializer list.

Sign up to request clarification or add additional context in comments.

10 Comments

Please stop spreading the term "initializer list" for this feature. Initializer lists are different.
Pray tell, what are "initializer lists"? To my knowledge, this is the correct term for this, and Google agrees...
@Tomalak: What is the correct term? "Initializer list" and "initialization list" are the only two I've ever heard or seen used. Important to get right, though, if you know different.
@Eli: This {1,2,3,4,5} is an initializer list.
Member Initializer List is the correct term to refer it, It is also commonly referenced as Initializer List, whether it is absolutely correct or not I do not know.
|
4

The benefit to your code is that ... well, what?! That is your code. The benefit is that it does what it says. Compared to what alternative? To your code being different? Different how?

myclass (unsigned int param) : param_ (param) {} 

The colon and following list is the ctor-initializer (do not call it "initializer list", which is something completely different in C++). It allows you to initialize members of the object.

Note that the following is not the same; you are only assigning to members after-the-fact:

myclass (unsigned int param) { param_ = param; } 

It won't really make a difference in this case, with an unsigned int, but once you start dealing with reference members and const members, and members with no default constructor, you find that you must initialise.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.