(C++)从派生类运算符调用基运算符

(C++) Invoking base operator from derived class operator

本文关键字:运算符 调用 派生 C++      更新时间:2023-10-16

从派生类的运算符中调用运算符的正确方法是什么?

我正在尝试从派生类的重载运算符中调用基重载运算符。我尝试了以下方法,但似乎不起作用:

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;
}