参考下面例子:
- #include<iostream>
- class Point
- {
- private:
- int x;
- int y;
- public:
- Point(int i = 0, int j = 0):x(i), y(j) {}
- /* 上面的初始化列表是可选的,因为也可以写成下面的形式:
- Point(int i = 0, int j = 0) {
- x = i;
- y = j;
- }
- */
- int getX() const {return x;}
- int getY() const {return y;}
- };
- int main()
- {
- Point t1(11, 22);
- std::cout<<”x = ”<<t1.getX()<<“, ”;
- std::cout<<”y = ”<<t1.getY();
- return 0;
- }
#include<iostream>
class Point
{
private:
int x;
int y;
public:
Point(int i = 0, int j = 0):x(i), y(j) {}
/* 上面的初始化列表是可选的,因为也可以写成下面的形式:
Point(int i = 0, int j = 0) {
x = i;
y = j;
}
*/
int getX() const {return x;}
int getY() const {return y;}
};
int main()
{
Point t1(11, 22);
std::cout<<"x = "<<t1.getX()<<", ";
std::cout<<"y = "<<t1.getY();
return 0;
}
x = 11, y = 22
上面代码是对初始化列表的简单演示. x和y也能在构造函数体中进行初始化。
但是,在某些情形下,数据成员不能在构造函数中初始化,而只能使用初始化列表。下面列出了这些情形:
1) non-static const 非静态常量数据成员的初始化:
const 数据成员必须使用初始化列表. 参考下面例子中的成员变量t.
|
输出:
111
2) 引用成员的初始化:
引用成员必须使用初始化列表. 参考下面例子中的’t’.
- #include<iostream>
- using namespace std;
- class Test
- {
- int &t;
- public:
- Test(int &t):t(t) {} //必须使用初始化列表
- int getT() { return t; }
- };
- int main()
- {
- int x = 22;
- Test t1(x);
- cout<<t1.getT()<<”, ”;
- x = 33;
- cout<<t1.getT()<<endl;
- return 0;
- }
#include<iostream>
using namespace std;
class Test
{
int &t;
public:
Test(int &t):t(t) {} //必须使用初始化列表
int getT() { return t; }
};
int main()
{
int x = 22;
Test t1(x);
cout<<t1.getT()<<", ";
x = 33;
cout<<t1.getT()<<endl;
return 0;
}
输出:
22, 33
3) 初始化没有默认构造函数的成员对象:
下面例子中, 类’B’ 的数据成员’a’是类’A’的一个对象, 并且’A’没有默认构造函数,则’B’必须使用初始化列表来对’a’进行初始化。
|
输出:
A’s Constructor called: Value of i: 10
B’s Constructor called
如果类A既有默认构造函数又有带参数的构造函数,当想要用默认构造函数来初始化’a’时, 则B不一定需要使用初始化列表;当想要用参数的构造函数来初始化’a’时,则B一定要使用初始化列表。
- #include <iostream>
- class A
- {
- int i;
- public:
- A(int);
- };
- A::A(int arg)
- {
- i = arg;
- std::cout << ”A’s Constructor called: Value of i: ” << i << std::endl;
- }
- // Class B 继承自Class A
- class B : A
- {
- public:
- B(int);
- };
- B::B(int x) : A(x)
- { //必须使用初始化列表
- std::cout << ”B’s Constructor called”;
- }
- int main()
- {
- B obj(10);
- return 0;
- }
#include <iostream>
class A
{
int i;
public:
A(int);
};
A::A(int arg)
{
i = arg;
std::cout << "A's Constructor called: Value of i: " << i << std::endl;
}
// Class B 继承自Class A
class B : A
{
public:
B(int);
};
B::B(int x) : A(x)
{ //必须使用初始化列表
std::cout << "B's Constructor called";
}
int main()
{
B obj(10);
return 0;
}
运行结果:A’s Constructor called: Value of i: 10
B’s Constructor called
- #include <iostream>
- class A
- {
- int i;
- public:
- A(int);
- int getI() const
- {
- return i;
- }
- };
- A::A(int i) : i(i) // 或者使用初始化列表,或者使用this指针
- {
- }
- /* 也可以写出下面代码
- A::A(int i) {
- this->i = i;
- }
- */
- int main()
- {
- A a(10);
- std::cout << a.getI();
- return 0;
- }
#include <iostream>
class A
{
int i;
public:
A(int);
int getI() const
{
return i;
}
};
A::A(int i) : i(i) // 或者使用初始化列表,或者使用this指针
{
}
/* 也可以写出下面代码
A::A(int i) {
this->i = i;
}
*/
int main()
{
A a(10);
std::cout << a.getI();
return 0;
}
输出:10
6) 性能原因:
- #include <iostream>
- class Type
- {
- public:
- Type()
- {
- std::cout << ”constructor called\n”;
- }
- ~Type()
- {
- std::cout << ”destructor called\n”;
- }
- Type(const Type & type)
- {
- std::cout << ”copy constructor called\n”;
- }
- Type& operator=(const Type & type)
- {
- std::cout << ”operator= called\n”;
- return *this;
- }
- };
- // 不使用初始化列表
- class MyClass
- {
- Type variable;
- public:
- MyClass(Type a) //假设Type是一个定义了拷贝构造与赋值操作符的类
- {
- variable = a;
- }
- };
- int main()
- {
- Type type;
- MyClass mc(type);
- return 0;
- }
#include <iostream>
class Type
{
public:
Type()
{
std::cout << "constructor called\n";
}
~Type()
{
std::cout << "destructor called\n";
}
Type(const Type & type)
{
std::cout << "copy constructor called\n";
}
Type& operator=(const Type & type)
{
std::cout << "operator= called\n";
return *this;
}
};
// 不使用初始化列表
class MyClass
{
Type variable;
public:
MyClass(Type a) //假设Type是一个定义了拷贝构造与赋值操作符的类
{
variable = a;
}
};
int main()
{
Type type;
MyClass mc(type);
return 0;
}
constructor called //main中的”Type type”
copy constructor called //使用新建的对象type来创建MyClass构造函数中的参数a
constructor called //MyClass的成员对象variable的构造
operator= called //MyClass构造函数中的”variable = a”
destructor called //a的生命周期结束destructor called //mc析构,其中成员对象variable也会析构
destructor called //type生命周期结束
1. 调用Type的拷贝构造函数来创建’a’.
2. 调用Type的构造函数来创建成员对象variable.
- // 使用初始化列表
- class MyClass {
- Type variable;
- public:
- MyClass(Type a):variable(a) { // 假设Type是一个定义了拷贝构造与赋值操作符的类
- }
- };
// 使用初始化列表
class MyClass {
Type variable;
public:
MyClass(Type a):variable(a) { // 假设Type是一个定义了拷贝构造与赋值操作符的类
}
};
constructor called
copy constructor called
copy constructor called
destructor called
destructor called
destructor called
使用初始化列表后,编译器会遵循下面顺序:
1. 调用Type的拷贝构造函数来初始化‘a’ .
2. 调用Type的拷贝构造函数,在初始化列表中的使用参数’a’来对成员对象”variable”进行初始化.
3. 调用Type的析构函数,因为a的声明周期结束了.
由上面的对比例子可知,使用初始化列表少用了一个步骤。即如果是在构造函数体中赋值,则需要copy constructor+constructor+assignment operator+destructor, 如果用初始化列表,则只需要copy constructor+copy constructor+destructor, 少了一个调用赋值运算符的过程。
在真实的应用程序中,如果存在很多的成员数据,则多出的这一个拷贝过程,会消耗掉可观的性能。