Can I overload pure virtual method in the base class?

前端 未结 2 1759
情深已故
情深已故 2021-01-18 08:17

In the example below I have a abstract class with pure virtual method (aka FUN1) and a normal method (aka FUN2).

#include 

class A
{
public:         


        
2条回答
  •  梦谈多话
    2021-01-18 08:54

    This is how derived class member lookup works: in the expression b.fun(), fun is first looked up in the scope of class B, and the lookup finds B::fun(int). So it stops and never finds A::fun().

    Relevant section of the standard is 10.2 [class.member.lookup]/4:

    If C contains a declaration of the name f, the declaration set contains every declaration of f declared in C that satisfies the requirements of the language construct in which the lookup occurs. (...) If the resulting declaration set is not empty, the subobject set contains C itself, and calculation is complete.

    To make the base class function directly accessible you can use a using declaration in the derived class, i.e. using A::fun;.

    For methods that are implemented in the base class an alternative is sometimes to qualify to call, i.e. b.A::fun().

提交回复
热议问题