Chinaunix首页 | 论坛 | 博客
  • 博客访问: 331551
  • 博文数量: 26
  • 博客积分: 1128
  • 博客等级: 少尉
  • 技术积分: 313
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-04 13:39
文章分类

全部博文(26)

文章存档

2012年(2)

2011年(10)

2010年(14)

分类: C/C++

2010-11-09 22:28:27

           Constructor Initialization list

  As we all known,we can initial our class member data in the constructor using the assignment operator.For example: 

#include <iostream>
using namespace std;

class M
{
    public:
        M (int m, int n)
        {
            a = m;
            b = n;
        }
    private:
        int        a;
        int        b;

}


 When the class's constructo is executed, the class member a and b are created. Then the body of the constructor is run, where the member data variable are assigned values. Attention, they are non-const variable, and pointer variable is ok.

Well,did you ever think about what would happen when we use const or reference variables as the class member? You should know that the const and reference variables must be initialized on the line as they are declared. I mean:

const int a;
    a = 1;                      //error

So We can't use it in the constructor as follows:

#include <iostream>
using namespace std;

class M
{
    public:
        M ()
        {
            a = 1;
        }
    private:
        const int        a;
};

Thus, we need another way to initialize them as they are declared. And this is done through using of an initialization list, which allows us to initialize member variables when they are created rather than afterwards.

Using an initialization list is very similar to doing implicit assignments.For instance:


#include <iostream>
using namespace std;

class M
{
    public:
        M () : a (1), b(2)
        { }
    private:
     int        a;
        int        b;
};

The initialization list is inserted after the constructor parameters, begin with a colon (:), and then lists each variable to initialize along with the value for that variable separated by a comma(,), Note that we no longer need to do the explicit assignments in the constructor body,since the initializer list replace that functionality. Also note that the initialization list doed not end in a semicolon (;).


Ok...Now you have already known the initialization list. There comes another question:

Are the class member variables initialized in accordence with the order of the initialization list?

Well..I supposed you to run the two programs showing as follow on your computer, and then you'll understand that...


Example 1: 

#include <iostream>
using namespace std;

class M
{
public:
    M (int i) : b(i), a(b)                              // we use i to initialize b

    {}

    void show ()
    {
        cout << a << endl;
        cout << b << endl;
    }
private:
    int        a;                                       //note that here a is first to be declared

    int        b;
};

int main (void)
{
    M    ob(1);

    ob.show ( );

    return 0;
}

Example 2:

#include <iostream>
using namespace std;

class M
{
public:
    M (int i) : b(a), a(i)                            //here we use i to initialize a

    {}

    void show ()
    {
        cout << a << endl;
        cout << b << endl;
    }
private:
    int        a;                                     //a is first to be declared

    int        b;
};

int main (void)
{
    M    ob(1);

    ob.show ( );

    return 0;
}

Example 1's run results:

14122996 //a random integer

1

Example 2's run results:

1

1

As the runing results showed: we can know that the class member variables initialized in accordence with the order of being declared in the class, but not the order of initialization list showing.



Did you get it?


阅读(1815) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-11-15 17:59:19

呼呼···这个是参考着一个英文教程写的···嘿嘿·· 应该不难理解。技术性文档阿···一般都没有什么修饰词··· 言简意赅··· 呵呵···

wwwzyf2010-11-15 17:07:46

额,纯英文的,我表示看不懂了。。嘿嘿,其实还可以