Virtual function implemented in base class not being found by compiler

我与影子孤独终老i 提交于 2019-12-01 02:27:29

You are hiding the method in the derived class. The simplest solution is to add a using declaration to the derived class.

struct Derived : public Base
{
    using Base::func;
    virtual void func( Two & );
};

The issue is that when the compiler tries to lookup the func identifier in the call d.func(one) it has to do that from Derived upwards, but it will stop in the first context where it finds the func identifier, which in this case it is Derived::func. No further lookup is performed and the compiler was seeing only the Derived::func( Two& ).

By adding the using Base::func; directive, when the compiler sees the Derived definition it brings all of Base::func declarations into scope, and it will find that there is a Base::func( One & ) that was not overridden in Derived.

Note also that if you were calling through a reference to Base, then the compiler would find both overloads of func and would dispatch appropriately each one to the final overrider.

Derived d;
Base & b = d;
b.func( one ); // ok even without the 'using Base::func;' directive

You hide func(One&) function in Derived. You could use fully qualified name:

d.Base::func( one );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!