1. 必须使用初始化列表初始化一个引用成员;
class CBase
{
public:
CBase(int &a) : m_a(a)
{
cout << "m_a的地址:" << &m_a << endl;
}
private:
int& m_a;
};
2.初始化派生类的基类时,必须使用初始化列表进行初始化;
class CBase
{
public:
CBase(int a)
{
cout << "a:" << a<< endl;
}
};
class Child :public CBase
{
public:
Child ():CBase(2)
{
}
Child (int a):CBase(a)
{
}
};
3.当初始化成员对象时,必须使用初始化列表进行初始化;
class CBase
{
public:
CBase(int a) : m_a(a)
{
}
private:
int m_a;
};
class CSub
{
public:
CSub() : m_obj(88) ,a(1)
{
cout << "this is m_obj::m_a = " << m_obj.m_a << endl;
}
private:
CBase m_obj;
int a;
};
4.必须使用初始化列表初始化一个常量成员;
class CBase
{
public:
CBase(int a) : m_a(a)
{
}
private:
const int m_a;
};