C++为什么复制构造函数参数必须为const?
#include<iostream>usingnamespacestd;classNum{private:inta;public:Num(intx=0):a(x){}Nu...
#include<iostream>using namespace std;class Num {private: int a;public: Num(int x = 0) :a(x) {} Num(const Num& a) { this->a = a.a; } friend Num operator+(Num x, Num y); friend ostream& operator<<(ostream &out, const Num& a);};Num operator+(Num x, Num y) { Num ans; ans.a = x.a + y.a; return ans;}ostream& operator<<(ostream &out, const Num& a) { out << "ans = " << a.a << endl; return out;}int main() { cout << Num(2) + 1 << endl; return 0;}复制构造函数参数不是const的话,main函数的加法运算无法执行
展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询