爲什麼這個代碼:爲什麼重載的賦值操作符不會被繼承?
class X {
public:
X& operator=(int p) {
return *this;
}
X& operator+(int p) {
return *this;
}
};
class Y : public X { };
int main() {
X x;
Y y;
x + 2;
y + 3;
x = 2;
y = 3;
}
給錯誤:
prog.cpp: In function ‘int main()’:
prog.cpp:14:9: error: no match for ‘operator=’ in ‘y = 3’
prog.cpp:14:9: note: candidates are:
prog.cpp:8:7: note: Y& Y::operator=(const Y&)
prog.cpp:8:7: note: no known conversion for argument 1 from ‘int’ to ‘const Y&’
prog.cpp:8:7: note: Y& Y::operator=(Y&&)
prog.cpp:8:7: note: no known conversion for argument 1 from ‘int’ to ‘Y&&’
爲什麼+
運營商繼承,但=
運營商呢?
回答你的問題是[這裏](http://stackoverflow.com/questions/12009865/operator-和函數 - 不是繼承在 - c) – 2013-03-01 13:36:56