C++:继承同名的重载非虚拟方法和虚拟方法会导致问题
我试图将两个具有不同参数列表的同名方法继承到派生类。其中之一是虚拟的并在派生类中被重写,另一个是非虚拟的。这样做时,我在尝试从派生类对象访问基类的非虚拟方法时收到编译错误。
代码片段
class Base {
public:
void f() {
cout << "[Base::f()]" << endl;
}
virtual void f(int arg) {
cout << "[Base::f(" << arg << ")]" << endl;
}
};
class Deriv : public Base {
public:
virtual void f(int arg) {
cout << "[Deriv::f(" << arg << ")]" << endl;
}
};
int main() {
Deriv d;
d.f(-1);
d.f(); // <<-- compile error
return 0;
}
这是产生以下编译错误的
:错误:没有匹配的函数可用于调用“Deriv::f()”
注意:候选者是:virtual void Deriv::f(int)
我不是 C++ 专家,但直到现在我认为成员方法可以通过其签名完全区分的假设是正确的。因此,非虚拟方法 Base::f() 不应被重写并且应保持可访问性。我这样做有错吗?
以下是一些有趣/附加的评论:
- - the overriding method Deriv::f(int arg) could be non-virtual as well; the error occurs in either way
- the error disappears/can be circumvented...
- ... by casting the Deriv object to the Base class
... when not overriding Base::f(int arg) in Deriv
... by adding the command "Base::f;" to the public part of Deriv
因此,由于我已经知道如何避免此编译错误,所以我主要对为什么发生此错误感兴趣!
I am trying to inherit two equally named methods with different parameter lists to a derived class. One of them is virtual and overridden in the derived class, the other one is non-virtual. Doing so, i get a compile error while trying to access the non-virtual method of the base class from an derived class object.
Here is the code snippet
class Base {
public:
void f() {
cout << "[Base::f()]" << endl;
}
virtual void f(int arg) {
cout << "[Base::f(" << arg << ")]" << endl;
}
};
class Deriv : public Base {
public:
virtual void f(int arg) {
cout << "[Deriv::f(" << arg << ")]" << endl;
}
};
int main() {
Deriv d;
d.f(-1);
d.f(); // <<-- compile error
return 0;
}
which produces the following compile error:
error: no matching function for call to ‘Deriv::f()’
note: candidates are: virtual void Deriv::f(int)
I am not an expert in C++, but until now I thought to be right in making the assumption that member methods can be completely distinguished by their signatures. Thus, the non-virtual method Base::f() should not be overridden and should remain accessible. Am I wrong with this?
Here are some interesting/additional comments on that:
- - the overriding method Deriv::f(int arg) could be non-virtual as well;
the error occurs in either way
- the error disappears/can be circumvented...
- ... by casting the Deriv object to the Base class
... when not overriding Base::f(int arg) in Deriv
... by adding the command "Base::f;" to the public part of Deriv
So, since I already know how to avoid this compile error, I am mainly interested in why this error happens!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
Deriv
中,添加以下内容:除了 @DumbCoder 给出的链接之外,您还可以在我对类似问题的回答中找到更多详细信息:在 C++ 中重写 Base 的重载函数
In
Deriv
, add this:In addition to the link given by @DumbCoder, you can find more details in my answer to a similar question: Overriding a Base's Overloaded Function in C++
派生类函数隐藏了基函数定义。 详细解释原因和如何
Derived class function hides the base function defintion. Detailed explaination as to why and how