复制构造函数给我加const!

This commit is contained in:
unlockable
2023-06-03 14:02:58 +08:00
parent 6a9bb5933a
commit d261d7a39e
2 changed files with 7 additions and 9 deletions

View File

@@ -9,7 +9,7 @@ public:
cout << "Constructor!";
display();
}
complex(complex &c) : r(c.r), i(c.i) {
complex(const complex &c) : r(c.r), i(c.i) {
cout << "Copy Constructor!";
display();
};
@@ -23,14 +23,11 @@ private:
};
complex complex::operator+(complex c2) {
complex result(r + c2.r, i + c2.i);
return result;
return complex(r + c2.r, i + c2.i);
}
complex complex::operator-(complex c2) {
complex result(r - c2.r, i - c2.i);
return result;
// return complex(r - c2.r, i - c2.i);
return complex(r - c2.r, i - c2.i);
}
void complex::display() {

View File

@@ -12,7 +12,7 @@ public: // 外部接口
cout << "Constructor!";
display();
}
complex(complex &c) : r(c.r), i(c.i) {
complex(const complex &c) : r(c.r), i(c.i) {
// this->num = ++count;
// cout << "(Num: " << this->num << ")";
@@ -37,9 +37,10 @@ private:
};
complex complex::operator+(complex c2) {
complex result(r + c2.r, i + c2.i);
return result;
// complex result(r + c2.r, i + c2.i);
// return result;
return complex(r + c2.r, i + c2.i);
// 原文return complex(r + c2.r, i + c2.i),不能编译=_=
}