【校招面试 之 C++】第1题 为什么优先使用构造函数的初始化列表

1.首先看一个例子:

#include<iostream>
using namespace std;


class Test1
{
public:
    Test1() // 无参构造函数
    { 
        cout << "Construct Test1" << endl ;
    }

    Test1(const Test1& t1) // 拷贝构造函数
    {
        cout << "Copy constructor for Test1" << endl ;
        this->a = t1.a ;
    }

    Test1& operator = (const Test1& t1) // 赋值运算符
    {
        cout << "assignment for Test1" << endl ;
        this->a = t1.a ;
        return *this;
    }
private:
    int a ;
};

class Test2
{
public:
    Test1 test1 ;
	// 情形1:不使用初始化参数列表
    //Test2(Test1 &t1)
    //{
    //    test1 = t1 ;
    //}

	// 情形2:使用初始化参数列表
	Test2(Test1 &t1):test1(t1){}
};
int main(){
	Test1 t1;
	Test2 t2(t1);
	system("pause");
	return 0;
}

 情形1输出:

 

情形2输出:

 第二种情况输出对应Test2的初始化列表,直接调用拷贝构造函数初始化test1,省去了调用默认构造函数的过程。所以一个好的原则是,能使用初始化列表的时候尽量使用初始化列表。提高了性能。

2. 除了性能问题之外,有些时场合初始化列表是不可或缺的,以下几种情况时必须使用初始化列表

  • 常量成员,因为常量只能初始化不能赋值,所以必须放在初始化列表里面
  • 引用类型,引用必须在定义的时候初始化,并且不能重新赋值,所以也要写在初始化列表里面
  • 没有默认构造函数的类类型,因为使用初始化列表可以不必调用默认构造函数来初始化,而是直接调用拷贝构造函数初始化。

 对于没有默认构造函数的类,我们看一个例子。

struct Test1
{
    Test1(int a):i(a){}
    int i ;
};

struct Test2
{
    Test1 test1 ;
    Test2(Test1 &t1)
    {
        test1 = t1 ;
    }
};

以上代码无法通过编译,因为Test2类中Test1 test1;需要调用默认的构造函数,但是Test1类没有无参的构造函数,但是由于Test1没有默认的构造函数,故而编译错误。正确的代码如下,使用初始化列表代替赋值操作。

struct Test2
{
    Test1 test1 ;
    Test2(Test1 &t1):test1(t1){}
}

成员变量的初始化顺序

 成员是按照他们在类中出现的顺序进行初始化的,而不是按照他们在初始化列表出现的顺序初始化的,看代码。

struct foo
{
    int i ;
    int j ;
    foo(int x):i(x), j(i){}; // ok, 先初始化i,后初始化j
};

猜你喜欢

转载自www.cnblogs.com/xuelisheng/p/9328387.html
今日推荐