(C++)从派生类运算符调用基运算符
(C++) Invoking base operator from derived class operator
从派生类的运算符中调用运算符的正确方法是什么?
我正在尝试从派生类的重载运算符中调用基重载运算符。我尝试了以下方法,但似乎不起作用:
const myObject& myObject::operator = (const myObject &otherObject) {
static_cast<BaseType>(*this) = static_cast<BaseType>(otherObject); // assuming BaseType has assignment overloaded
return *this;
}
这是实际代码:
const studentType& studentType::operator = (const studentType &student) {
static_cast<personType>(*this) = static_cast<personType>(student);
// new junk here
return *this;
}
注意:基类的运算符已重载,因此没有问题。
使用以下代码
const studentType& studentType::operator = (const studentType &student) {
personType::operator =(student);
// new junk here
return *this;
}
相关文章:
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 使用运算符调用 void 函数时出错<set>
- C++:使用运算符 = 调用多参数构造函数
- 重载模板运算符调用单独的类运算符
- 为什么转换运算符调用复制构造函数两次,而等效函数只调用它一次
- C++ - 从移动分配运算符调用复制分配
- 从赋值运算符调用构造函数
- C++ 为什么运算符>会中断嵌套的运算符()调用?
- 显式运算符 = 调用和 = 运算符之间是否有区别
- 全局新运算符调用语法
- (C++)从派生类运算符调用基运算符
- 初始化对象后,用隐式转换而不是赋值运算符调用构造函数有什么意义
- 函数可以使用点运算符调用另一个功能
- 为什么这个运算符=调用不明确
- 对重载运算符调用QMetaObject
- 重载unique_ptr运算符调用make_unique
- 对于什么对象指针值,指向成员的指针运算符调用未定义的行为
- 从重载的下标运算符调用虚函数
- 从运算符调用纯虚函数<<
- 可变参数扩展是否可以用作逗号运算符调用链