作用:
C++提供了初始化列表语法,用来初始化属性
语法:构造函数():属性1(值1),属性2(值2)... {}
#include <bits/stdc++.h>
using namespace std;
class Person{
public:
Person(){
cout << "Person默认构造函数的调用" << endl;
}
Person(int num):m_Num(num){
}
~Person(){
cout << "Person析构函数的调用" << endl;
}
public:
int m_Num;
};
Person func(Person per){
return per;
}
int main(){
Person person2(10); // 括号法调用有参构造函数
cout << "m_Num = " << person2.m_Num << endl;
return 0;
}