简体   繁体   English

C++:继承重载的非虚方法和虚方法都具有相同的名称会导致问题

[英]C++: inheriting overloaded non-virtual method and virtual method both with the same name causes problem

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()'错误:没有用于调用“Deriv::f()”的匹配函数
note: candidates are: virtual void Deriv::f(int)注意:候选人是: 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.我不是 C++ 的专家,但直到现在我认为假设成员方法可以通过它们的签名完全区分是正确的。 Thus, the non-virtual method Base::f() should not be overridden and should remain accessible.因此,非虚拟方法 Base::f() 不应被覆盖并且应保持可访问性。 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; - 覆盖方法 Deriv::f(int arg) 也可以是非虚拟的; the error occurs in either way 错误以任何一种方式发生
    - the error disappears/can be circumvented... - 错误消失/可以规避...
      ... by casting the Deriv object to the Base class ...通过将 Deriv 对象转换为 Base 类
      ... when not overriding Base::f(int arg) in Deriv ...在 Deriv 中不覆盖 Base::f(int arg) 时
      ... by adding the command "Base::f;" ...通过添加命令“Base::f;” to the public part of Deriv 到 Deriv 的公共部分

So, since I already know how to avoid this compile error, I am mainly interested in why this error happens!所以,既然我已经知道如何避免这个编译错误,我主要感兴趣的是为什么会发生这个错误!

In Deriv , add this:Deriv ,添加以下内容:

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++除了@DumbCoder 提供的链接之外,您还可以在我对类似问题的回答中找到更多详细信息: Overriding a Base's Overloaded Function in C++

Derived class function hides the base function defintion.派生类函数隐藏了基函数定义。 Detailed explaination as to why and how关于原因方式的详细解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 覆盖 C++ 中的非虚拟方法 - Override a non-virtual method in C++ C++ 在界面中添加非虚拟static方法正常吗? - C++ Add non-virtual static method in interface is normal? 模拟非虚方法C ++(gmock) - Mock non-virtual method C++ (gmock) 为非虚方法指定的c ++初始化 - c++ initialized specified for non-virtual method 在C ++编译期间,“具有虚拟方法......但非虚拟析构函数”警告意味着什么? - What does 'has virtual method … but non-virtual destructor' warning mean during C++ compilation? 调用非虚拟基方法时,C++ 中的虚拟 inheritance 是否有任何惩罚/成本? - Is there any penalty/cost of virtual inheritance in C++, when calling non-virtual base method? 虚方法但非虚析构函数 - Virtual method but non-virtual destructor 非内联非虚方法添加到类的实例的字节数是多少? C ++ - How many bytes does a non-inline non-virtual method add to the instance of a class? C++ 派生类中非虚拟函数的C ++同名与`final`说明符冲突 - C++ same name for non-virtual function in derived class conflicts with `final` specifier 如何模拟一个方法(非虚拟)在C ++中使用GMock返回特定值? - How to Mock A Method(non-virtual) To Return Particular Value Using GMock in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM