这个问题已经在这里有一个答案:
- 麻烦操作者的继承=在C ++ 5个答案
- 运算符=和未用C继承功能++? 3个回答
为什么这样的代码:
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&&’
为什么+
运营商继承,但=
运营商呢?