【问题标题】:Derived class not inheriting overloaded method from base class派生类不从基类继承重载方法
【发布时间】:2014-12-23 05:17:52
【问题描述】:

我希望基类中的方法调用将在派生类中实现的纯虚拟方法。但是,派生类似乎没有继承基类的无参数方法。我究竟做错了什么?编译器是 MSVC12。

错误 C2660: 'Derived::load' : 函数不接受 0 个参数

这是一个完整的示例(由于错误而无法编译):

struct Base
{
    void load() { load(42); }; // Making this virtual doesn't matter.
    virtual void load(int i) = 0;
};

struct Derived : Base
{
    virtual void load(int i) {};
};

int main()
{
    Derived d;
    d.load(); // error C2660: 'Derived::load' : function does not take 0 arguments
}

【问题讨论】:

标签: c++ inheritance polymorphism overloading shadowing


【解决方案1】:

哦,派生类确实继承void load()

但是你在派生类中声明void load(int i),这意味着它被遮蔽了。

using Base::load; 添加到Derived 以将Baseload 的所有非覆盖定义添加到Derived 中的重载集。

或者,使用 scope-resolution-operator d.Base::load(); 显式调用 Base-class-version。

【讨论】:

    【解决方案2】:

    您必须明确调用Based.Base::load();。我不知道为什么,但它有效。我的猜测是覆盖隐藏了所有重载。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多