1)参数列表的初始化顺序和初始化列表列出的顺序无关,只与它在类中声明顺序有关
2)
#include <stdio.h>
#include <iostream>
using namespace std;
class Parent
{
public:
int m_a;
int m_b;
public:
Parent(int b): m_b(b), m_a(m_b) ///
{
}
void Show()
{
cout << m_a << " " << m_b << endl;
}
};
int main()
{
Parent P(100);
P.Show();
while(1);
return 0;
}
构造函数等价于
Parent(int b)
{
m_a = m_b; //此时m_b的值并不确定
m_b = b;
}
3)const变量必须由参数化列表去初始化
class Array
{
public:
const int m_len;
int *m_arr;
public:
Array(int len):m_len(len)
{
m_arr = new int[m_len];
}
void Show()
{
cout << m_len << endl;
}
void Print()
{
cout << m_arr[0] << endl;
}
};
int main()
{
Array A(4);
A.Show();
A.m_arr[0] = 10;
A.Print();
while(1);
return 0;
}