作用:
C++提供了初始化列表语法,用来初始化属性
语法:构造函数():属性1(值1),属性2(值2)... {}
#include<iostream>
using namespace std;
class Person
{
public:
//传统初始化操作
/*Person(int a, int b, int c)
{
_A = a;
_B = b;
_C = c;
}*/
//初始化列表初始化属性
Person(int a,int b,int c) :_A(a), _B(b), _C(c)
{
}
int _A;
int _B;
int _C;
};
void test01()
{
//Person p(10, 20, 30);
Person p(30,20,10);
cout << p._A << " " << p._B << " " << p._C << endl;
}
int main()
{
test01();
return 0;
}
标签:初始化,20,int,列表,Person,属性
From: https://blog.csdn.net/m0_74863573/article/details/142501645