Can I overload pure virtual method in the base class?

前端 未结 2 1733
情深已故
情深已故 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-18 08:32

    Try to add using A::fun; statement in B class :

    #include 
    
    class A
    {
    public:
        virtual void fun(int i) = 0; // FUN1
        void fun() { this->fun(123); } // FUN2
    };
    
    class B : public A
    {
    public:
        using A::fun;
        virtual void fun(int i) { std::cerr << i << std::endl; }
    };
    
    int main(int, char**)
    {
        B b;
        b.fun();
        b.fun(5);
    }
    

提交回复
热议问题