C++提供了初始化列表语法,来初始化属性
构造函数的初始化列表
语法:构造函数(): 属性1(值1),属性2(值2)……{}
#include <iostream>
using namespace std;
class Person
{
public:
Person(int a,int b,int c) //传统初始化操作
{
m_a =a;
m_b =b;
m_c =c;
}
Person(int a,int b,int c):m_a(a),m_b(b),m_c(v) //用初始化列表
{
}
int m_a;
int m_b;
int m_c;
};
void test01()
{
// Person p(10,20,30); //传统初始化
Person p(30,20,10); //初始化列表调用
cout << "m_a" << p.m_a << endl;
cout << "m_b" << p.m_b << endl;
cout << "m_c" << p.m_c << endl;
}