本文介绍了(C++) 从派生类运算符调用基运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从派生类的运算符中调用运算符的正确方法是什么?
What's the correct way to invoke an operator from within the derived class's operator?
我正在尝试从派生类的重载运算符中调用基重载运算符.我尝试了以下但似乎不起作用:
I'm trying to call a base overloaded operator from within the derived class's overloaded operator. I tried the following but it doesn't seem to work:
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;
}
注意:基类的运算符已重载,因此没有问题.
Note: The base class's operator is overloaded, so no issues there.
推荐答案
使用以下代码
const studentType& studentType::operator = (const studentType &student) {
personType::operator =(student);
// new junk here
return *this;
}
这篇关于(C++) 从派生类运算符调用基运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!