初始化列表给对象赋初值文章来源:https://www.uudwc.com/A/XkyAz/
#include <iostream>
#include <string.h>
using namespace std;
//初始化列表
//构造函数
class Persion {
public:
Persion(int a, int b, int c) :m_A(a), m_B(b), m_C(c)
{
cout << "携参构造函数列表的方式初始化对象属性" << "a:" << this->m_A << endl;
}
Persion() :m_A(10), m_B(20), m_C(30)
{
cout << "无参构造函数列表的方式初始化对象属性" << "a:" << this->m_A << endl;
}
int m_A;
int m_B;
int m_C;
};
void main()
{
Persion p1;
cout << "p1.m_A:" << p1.m_A << endl;
Persion p2(100, 200, 300);
cout << "p2.m_A:" << p2.m_A << endl;
}
文章来源地址https://www.uudwc.com/A/XkyAz/