C++11的关键字final有两个用途
- 禁止基类被继承。
class Base final { }; // error,Derive不能从Base派生。 class Derive : public Base { };
- 禁止虚函数被重写。
struct Base { virtual void func() final; // Base::func is final // void func2() final; // Error: non-virtual function cannot be final }; struct Derive final : Base { // struct Derive is final // void func(); // Error: func cannot be overridden as it's final in Base