3

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?

Community
  • 1
  • 1
Bitmap
  • 12,402
  • 16
  • 64
  • 91

2 Answers2

12
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.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Please stop spreading the term "initializer list" for this feature. Initializer lists are different. – Lightness Races in Orbit Jul 17 '11 at 15:01
  • Pray tell, what are "initializer lists"? To my knowledge, this is the correct term for this, and Google agrees... – Eli Iser Jul 17 '11 at 15:06
  • @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. – Cody Gray - on strike Jul 17 '11 at 15:12
  • 1
    @Eli: This `{1,2,3,4,5}` is an initializer list. – Benjamin Lindley Jul 17 '11 at 15:13
  • @Benjamin: thanks. But I join Cody with the follow-up question. – Eli Iser Jul 17 '11 at 15:14
  • The term in the grammar is `ctor-initializer`, which I employ. I'm not aware of any established colloquial phrase; however, your apparent invention, Als, of "member initializer list" seems appropriate to me. – Lightness Races in Orbit Jul 17 '11 at 15:15
  • 1
    `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. – Alok Save Jul 17 '11 at 15:19
  • *"Your class doesn't have a default constructor"* -- Perhaps you should change that to say *"Your class has a member which doesn't have a default constructor"* – Benjamin Lindley Jul 17 '11 at 15:39
  • `ctor-initializer` includes the colon; `mem-initializer-list` is the actual list, so Als's use is hardly an invention. However, regardless of what the grammar has to say on the subject, I've never seen anybody get confused by overloading the generic term "initializer list" to refer to both constructs, so I see no reason to get so fussy. C++11 might change that, though. – Dennis Zickefoose Jul 17 '11 at 15:51
  • @Dennis: There's simply no reason to use conflicting, ambiguous terminology when there's a _chance_ it will confuse somebody. Consider a language newcomer, for example. Why pick a silly term, when you can pick one that's both unambiguous and evidently written in the standard? It's win-win! :) – Lightness Races in Orbit Jul 17 '11 at 15:54
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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055