有时候我们会定义这样一种类:我们不希望其他类继承它,或者不想考虑它是否适合作为一个基类,为了实现这样一个目的,c++11提供了一种防止继承发生的方法,即在类名后面跟一个关键字final。
另外,我们也可以在成员函数的后面加final。
如果我们把某个函数定义成final了,则之后任何尝试覆盖该函数的操作都将引发错误。
struct A{
virtual void f1(int) const;
virtual void f2();
void f3;
};
struct B1 : A {
void f1(int) const final; //不允许后续的其他类覆盖f1(int)
};
struct B2 : B1 {
void f1(int) const; //错误,B1已经将f1()声明成final
};