Overload of pure virtual function

后端 未结 6 628
无人及你
无人及你 2021-01-12 23:45

I usually use pure virtual functions for those methods that are required by my code to work well. Therefore, I create interfaces and then other users implement their derived

6条回答
  •  自闭症患者
    2021-01-13 00:09

    These 2 functions are different. The latter is not overriding the first

    virtual void foo(int,double)=0;
    virtual void foo(int, double, double);
    

    The second one is new virtual function specific to derived.

    If you put a override at the end the compile will complain that you are not overriding anything. This is c++11 check though.

    virtual void foo(int, double, double) override;
    

    The user can override a pure virtual function to confirm use override at the end of function to verify. In your case the second function can only be accessed using Derived pointer or type. (although it cannot be instantiated unless the pure virtual function is properly overridden and implemented, untill then it is an abstract class). Hence if it is not to be intended to be overidden further by classes that derives from Derived then making it virtual is a overhead as anyway it is not overidding the base method.

提交回复
热议问题