#include <iostream>
using namespace std;
class Sample {
public:
int v;
Sample(int n=0){
v=n;
}
Sample(const Sample & m){
v=m.v+2;
}
};
void PrintAndDouble(Sample o)
{
cout << o.v;
cout << endl;
}
int main()
{
Sample a(5);
Sample b = a;
PrintAndDouble(b);
Sample c = 20;
PrintAndDouble(c);
Sample d;
d = a;
cout << d.v;
return 0;
}
为什么构造函数的形参要赋值n=0
那个是默认参数,当你调用构造函数新建对象时,如果不传参数,那么n就默认初始化为0