这道题为什么参数里加上const就对了?
#include <iostream>using namespace std;
class Sample {
public:
int v;
Sample(int n=5){v=n;}
Sample(const Sample &t){
v=t.v+2;
}
};
void PrintAndDouble(Sample o)
{
cout << o.v;
cout << endl;
}
int main()
{
Sample a(5);
Sample b = a;
PrintAndDouble(b);
Sample c = 20; //上面红色那个const不加的话,这一句会报错,求大佬解释一下
PrintAndDouble(c);
Sample d;
d = a;
cout << d.v;
return 0;
}
[此贴子已经被作者于2018-9-15 15:24编辑过]