C++:初始化列表
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() : m_A(10), m_B(20), m_C(30) {
}
//更灵活的写法
/*Person(int a, int b, int c) :m_A(a), m_B(b), m_C(c) {
}*/
int m_A;
int m_B;
int m_C;
};
void test01() {
// Person p(10, 20, 30);
Person p;//创建一个对象
cout << "a b c = " << p.m_A << p.m_B << p.m_C << endl;
}
int main(){
test01();
system("pause");
return 0;
}
posted on 2022-04-16 09:47 Michael_chemic 阅读(28) 评论(0) 编辑 收藏 举报