前缀和后缀操作符继承
Prefix and postfix operators inheritance
考虑以下代码:
// 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;
}
我尝试创建一个类的层次结构。基类,只实现前缀自增/自减操作符。并在派生类中添加后缀版本。
但是,编译器找不到派生类对象的前缀操作。
为什么会发生这种情况,为什么前缀操作符不被继承?
执行以下命令,导入prefix
类中隐藏的operator--
和operator++
。
struct any : prefix
{
using prefix::operator--;
using prefix::operator++;
any operator--(int) { return any(); }
any operator++(int) { return any(); }
};
现场演示
这可能是一个糟糕的想法,但至少它可以编译。
相关文章:
- 继承函数的重载解析
- 继承期间显示未知行为的子类
- 头文件-继承c++
- 为什么在保护模式下继承升级不起作用
- 通过继承类使用来自不同命名空间的运算符
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 混合组合和继承的C++问题
- c++类继承和赋值操作符
- 带有抽象继承的后缀操作符++
- 带有继承的c++操作符重载
- 在继承std::vector的类中使用操作符[]
- 前缀和后缀操作符继承
- 是否可以继承赋值操作符?
- c++如何继承返回特定类型的操作符
- c++中操作符的继承
- 在抽象类层次结构中操作符=的复杂继承
- 继承类的操作符重载
- 通过继承专门化操作符模板
- c++,通过指针传递泛型数组,继承,错误:没有接受右操作数的操作符
- c++赋值操作符关于继承的问题