我想问一下如何在派生类重写函数中调用隐藏的Base类操作符函数,下面是我的代码,注释行是问题。
class Base{
public:
virtual bool operator^(Base &b){
cout << "hehe" << endl;
return true;
}
virtual void fn() = 0;
};
class Dev: public Base{
public:
virtual bool operator^(Base &b){
// how to call operator ^ in the Base class here??
cout << "haha" << endl;
return false;
}
virtual void fn(){}
};
答案 0 :(得分:3)
使用运营商的限定名称。
例如
#include <iostream>
using namespace std;
class Base{
public:
virtual bool operator^(Base &b){
cout << "hehe" << endl;
return true;
}
virtual void fn() = 0;
};
class Dev: public Base{
public:
virtual bool operator^(Base &b){
Base::operator^( b );
cout << "haha" << endl;
return false;
}
virtual void fn(){}
};
int main()
{
Dev v;
Base &b = v;
v ^ b;
return 0;
}
程序输出
hehe
haha
答案 1 :(得分:0)
You can also do an explicit upcast of *this
:
class Dev: public Base{
public:
virtual bool operator^(Base &b)
{
static_cast<Base&>(*this) ^ b;
cout << "haha" << endl;
return false;
}
virtual void fn(){}
};