初始化列表
- 初始化列表:和构造函数功能类似,也是用来初始化对象属性的。
比较简单,简单举一个例子来进行说明
#include <iostream>
using namespace std;
class Person
{
private:
int a;
int b;
int c;
public:
Person() : a(10), b(20), c(30)
{
}
Person(int a1, int b1, int c1) : a(a1), b(b1), c(c1)
{
}
int getA()
{
return a;
}
int getB()
{
return b;
}
int getC()
{
return c;
}
};
int main()
{
Person p;
cout << "a= " << p.getA() << endl;
cout << "b= " << p.getB() << endl;
cout << "c= " << p.getC() << endl;
Person p2(40, 50, 60);
cout << "a= " << p2.getA() << endl;
cout << "b= " << p2.getB() << endl;
cout << "c= " << p2.getC() << endl;
return 0;
}
|