Dra*_*neg 1 c++ inheritance overriding c++11
override关键字允许确保该函数将被覆盖.我正在寻找相反的功能.所以 - 当我写一个新函数时 - 我想用它来标记它以确保它不会被意外覆盖.
(另外,我不想做它static,因为它看起来像属于一个对象而不是类)
我想用一些东西来标记,以确保它不会被意外覆盖.
您可以使用说明final符.来自cppreference的示例:
struct Base
{
virtual void foo();
};
struct A : Base
{
void foo() final; // A::foo is overridden and it is the final override
void bar() final; // Error: non-virtual function cannot be overridden or be final
};
Run Code Online (Sandbox Code Playgroud)
如果您不希望在派生类中重写虚函数,则可以使用final:
指定无法在派生类中重写虚函数,或者无法继承类.
例如
struct Base
{
virtual void foo() final; // foo cannot be overridden in the derived class
};
struct Derived : Base
{
void foo(); // Error: foo cannot be overridden as it's final in Base
};
Run Code Online (Sandbox Code Playgroud)