小结
- 使用初始化列表能够提高程序运行效率。
- 除了性能问题之外,有些时候合初始化列表是不可或缺的,以下几种情况时必须使用初始化列表
const
常量成员,因为常量只能初始化不能赋值,所以必须放在初始化列表里面- 引用类型,引用必须在定义的时候初始化,并且不能重新赋值,所以也要写在初始化列表里面
- 没有默认构造函数的类类型,因为使用初始化列表可以不必调用默认构造函数来初始化,而是直接调用拷贝构造函数初始化
一、初始化列表的定义
构造函数和其他函数不同,构造函数除了有名字、参数列表和函数体外,还可以有初始化列表,即以冒号开头,后跟一系列以逗号分隔的初始化字段,如下的foo
类。
class foo{
public:
foo(string s, int i):name(s), id(i){ } ; // 初始化列表
private:
string name ;int id ;
};
- 构造函数的执行分为2个阶段:
- 初始化阶段:所有类类型(class type)的成员都会在初始化阶段初始化,即使该成员没有出现在构造函数的初始化列表中。
- 计算阶段:一般用于执行构造函数体内的赋值操作。
二、构造函数的两个阶段
下面用到的this
指针,C++中的每一个对象都能通过this
指针访问自己的地址,this
指针是所有成员函数的隐含参数,所以在成员函数内部,可以用来指向调用对象。
PS:友元函数没有this
指针,因为友元函数不是类的成员,只有成员函数才有this
指针。
#include <iostream>
using namespace std;
class Box{
public:
// 构造函数定义
Box(double l=2.0, double b=2.0, double h=2.0)
{
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
}
double Volume(){
return length * breadth * height;
}
int compare(Box box){
return this->Volume() > box.Volume();
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
int main(void){
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
if(Box1.compare(Box2)){
cout << "Box2 is smaller than Box1" <<endl;
}
else{
cout << "Box2 is equal to or larger than Box1" <<endl;
}
return 0;
}
先来看构造函数函数的2个阶段:初始化和计算。
#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;}
int a ;
};
class Test2{
public:
Test1 test1 ;
Test2(Test1 &t1)
{test1 = t1 ;}
};
int main(){
Test1 t1;
Test2 t2(t1);
system("pause");
return 0;
}
分析上面的结果:
(1)第一行:Test1 t1;
构造一个Test1
对象;
(2)第二行:输出对应Test2
构造函数中的代码,用默认的构造函数初始化对象test1 // 这就是所谓的初始化阶段
(3)第三行:输出对应Test2
的赋值运算符,对test1执行赋值操作 // 这就是所谓的计算阶段
三、使用初始化列表的原因
初始化类的成员有两种方式,一是使用初始化列表,二是在构造函数体内进行赋值操作。
主要是性能问题,对于内置类型,如int
, float
等,使用初始化类表和在构造函数体内初始化差别不是很大,但是对于类类型来说,最好使用初始化列表,为什么呢?
由下面的测试可知,使用初始化列表少了一次调用默认构造函数的过程,这对于数据密集型的类来说,是非常高效的。
class Test2{
public:
Test1 test1 ;
Test2(Test1 &t1):test1(t1){}
}
完整代码如下:
#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;}
int a ;
};
class Test2{
public:
Test1 test1 ;
Test2(Test1 &t1):test1(t1){}
};
int main(){
Test1 t1;
Test2 t2(t1);
system("pause");
return 0;
}
分析上面的结果:
(1)第一行输出对应 调用代码的第一行
(2)第二行输出对应Test2
的初始化列表,直接调用拷贝构造函数初始化test1,省去了调用默认构造函数的过程。
所以一个好的原则:能使用初始化列表的时候尽量使用初始化列表。
四、必须使用初始化列表的时候
除了性能问题之外,有些时候合初始化列表是不可或缺的,以下几种情况时必须使用初始化列表
const
常量成员,因为常量只能初始化不能赋值,所以必须放在初始化列表里面- 引用类型,引用必须在定义的时候初始化,并且不能重新赋值,所以也要写在初始化列表里面
- 没有默认构造函数的类类型,因为使用初始化列表可以不必调用默认构造函数来初始化,而是直接调用拷贝构造函数初始化
class Test1{
public:
Test1(int a):i(a){}
int i;
};
class Test2{
public:
Test1 test1 ;
Test2(Test1 &t1)
{test1 = t1 ;}
};
以上代码无法通过编译,因为Test2的构造函数中test1 = t1
这一行实际上分成两步执行:
调用 Test1 的默认构造函数来初始化 test1
由于Test1没有默认的构造函数,所以1 无法执行,故而编译错误。
正确的代码如下,使用初始化列表代替赋值操作:
class Test2{
public:
Test1 test1 ;
Test2(int x):test1(x){}
}
五、成员变量的初始化顺序
成员是按照他们在类中出现的顺序进行初始化的,而不是按照他们在初始化列表出现的顺序初始化的:
class foo{
public:
int i ;int j ;
foo(int x):i(x), j(i){}; // ok, 先初始化i,后初始化j
};
再比如:
class foo{
public:
int i ;int j ;
foo(int x):j(x), i(j){} // i值未定义
};
这里i
的值是未定义的因为虽然j
在初始化列表里面出现在i
前面,但是i
先于j
定义,所以先初始化i
,而i
由j
初始化,此时j
尚未初始化,所以导致i
的值未定义。一个好的习惯是,按照成员定义的顺序进行初始化。