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

时间:2014-02-23 01:08:10

标签: c++ polymorphism operator-overloading

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

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

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

注意:基类的运算符已经过载,所以没有问题。

1 个答案:

答案 0 :(得分:0)

使用以下代码

const studentType& studentType::operator = (const studentType &student) {
  personType::operator =(student);

  // new junk here

  return *this;
}
相关问题