C++:继承同名的重载非虚拟方法和虚拟方法会导致问题

发布于 2024-09-13 01:10:30 字数 1264 浏览 7 评论 0原文

我试图将两个具有不同参数列表的同名方法继承到派生类。其中之一是虚拟的并在派生类中被重写,另一个是非虚​​拟的。这样做时,我在尝试从派生类对象访问基类的非虚拟方法时收到编译错误。

代码片段

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄与心 2024-09-20 01:10:30

Deriv 中,添加以下内容:

using Base::f;

除了 @DumbCoder 给出的链接之外,您还可以在我对类似问题的回答中找到更多详细信息:在 C++ 中重写 Base 的重载函数

In Deriv, add this:

using Base::f;

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++

情话已封尘 2024-09-20 01:10:30

派生类函数隐藏了基函数定义。 详细解释原因如何

Derived class function hides the base function defintion. Detailed explaination as to why and how

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文