<分区>
考虑以下代码:
// Only prefix operators
struct prefix
{
prefix& operator--() { return *this; }
prefix& operator++() { return *this; }
};
// Try to represent prefix & postfix operators via inheritance
struct any : prefix
{
any operator--(int) { return any(); }
any operator++(int) { return any(); }
};
int main() {
any a;
a--;
a++;
--a; // no match for ‘operator--’ (operand type is ‘any’)
++a; // no match for ‘operator++’ (operand type is ‘any’)
return 0;
}
我试图创建类的层次结构。基类,只实现前缀自增/自减运算符。并在派生类中添加后缀版本。
但是,编译器无法找到派生类对象的前缀操作。
为什么会这样,为什么前缀运算符不被继承?