Delete virtual function from a derived class

后端 未结 5 1996
萌比男神i
萌比男神i 2021-01-30 09:06

I have a virtual base class function which should never be used in a particular derived class. Is there a way to \'delete\' it? I can of course just give it an empty definitio

5条回答
  •  鱼传尺愫
    2021-01-30 09:42

    I have a virtual base class function which should never be used in a particular derived class.

    C++11 provides a keyword final which prevents a virtual function being overriden from.

    Look: http://en.cppreference.com/w/cpp/language/final .

    class B
    {
      virtual void f() final;
    };
    
    class D : public B
    {
      // virtual void f();  // a compile-time error
      // void f() override; // a compile-time error
      void f(); // non-virtual function, it's ok
    };
    

提交回复
热议问题