【发布时间】:2012-07-14 00:22:46
【问题描述】:
如果在 C++ 中的基类和派生类中都重载了基类中的方法,我似乎不能直接使用派生类中的方法。以下代码产生错误no matching function for call to ‘Derived::getTwo()’。
class Base {
public:
int getTwo() {
return 2;
}
int getTwo(int, int) {
return 2;
}
};
class Derived : public Base {
public:
int getValue() {
// no matching function for call to ‘Derived::getTwo()’
return getTwo();
}
int getTwo(int) {
return 2;
}
};
如果我将return getTwo(); 更改为return ((Base*) this)->getTwo(),它可以工作,但对我来说这看起来很难看。我该如何解决这个问题?
附:如果这很重要,我会使用带有选项 std=gnu++c11 的 g++ 4.7。
【问题讨论】:
-
这肯定会作为副本关闭,但与此同时,快速的答案是将
using Base::getTwo添加到您在类范围内的Derived定义中。
标签: c++ class inheritance methods