member (" : ") syntax in the constructor?

Recently I've seen an example like the following:

#include <iostream>

class Foo {
public:
  int bar;
  Foo(int num): bar(num) {};
};

int main(void) {
  std::cout << Foo(42).bar << std::endl;
  return 0;
}

What does this strange : bar(num) mean? It somehow seems to initialize the member variable but I've never seen this syntax before. It looks like a function/constructor call but for an int ? Makes no sense for me. Perhaps someone could enlighten me. And, by the way, are there any other esoteric language features like this, you'll never find in a ordinary C++ book?


It's a member initialization list. You should find information about it in any good C++ book.

You should, in most cases, initialize all member objects in the member initialization list (however, do note the exceptions listed at the end of the FAQ entry).

The takeaway point from the FAQ entry is that,

All other things being equal, your code will run faster if you use initialization lists rather than assignment.


Foo(int num): bar(num)    

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

Simply said, it initializes your member bar to a value num .


What is the difference between Initializing and Assignment inside a constructor?

Member Initialization:

Foo(int num): bar(num) {};

Member Assignment:

Foo(int num)
{
   bar = num;
}

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

When you initialize fields via Member initializer list the constructors will be called once and the object will be constructed and initialized in one operation.

If you use 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.

Cost of Member Initialization = Object Construction 
Cost of Member Assignment = Object Construction + Assignment

The latter is actually equivalent to:

Foo(int num) : bar() {bar = num;}

While the former is equivalent to just:

Foo(int num): bar(num){}

For an inbuilt (your code example) 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 non static const member or
  • Your class member doesn't have a default constructor or
  • For initialization of base class members or
  • When constructor's parameter name is same as data member(this is not really a MUST)

  • A code example:

    class MyClass
    {
        public:
            //Reference member, has to be Initialized in Member Initializer List
            int &i;       
            int b;
            //Non static const member, must be Initialized in Member Initializer List
            const int k;  
    
        //Constructor’s parameter name b is same as class data member 
        //Other way is to use this->b to refer to data member
        MyClass(int a, int b, int c):i(a),b(b),k(c)
        {
             //Without Member Initializer
             //this->b = b;
        }
    };
    
    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.
  • Base class MyClass does not have a default constructor, So to initialize its member one will need to use Member Initializer List.
  • Online version of code sample .


    Important points to Note while using Member Initializer Lists:

    Class Member variables are always initialized in the order in which they are declared in the class.

    They are not initialized in the order in which they are specified in the Member Initalizer List.
    In short, Member initialization list does not determine the order of initialization.

    Given the above it is always a good practice to maintain the same order of members for Member initialization as the order in which they are declared in the class definition. This is because compilers do not warn if the two orders are different but a relatively new user might confuse member Initializer list as the order of initialization and write some code dependent on that.


    That's constructor initialisation. It is the correct way to initialise members in a class constructor, as it prevents the default constructor being invoked.

    Consider these two examples:

    // Example 1
    Foo(Bar b)
    {
       bar = b;
    }
    
    // Example 2
    Foo(Bar b)
       : bar(b)
    {
    }
    

    In example 1:

    Bar bar();  // default constructor
    bar = b;  // assignment
    

    In example 2:

    Bar bar(b) // copy constructor
    

    It's all about efficiency.

    链接地址: http://www.djcxy.com/p/40234.html

    上一篇: 用++来增加Python整数

    下一篇: member(“:”)语法在构造函数中?