我读一个真棒awesome C++11 tutorial和作者提供了这个例子,同时解释final
关键字:是否有意义最终关键字添加到虚拟功能在没有基类的类(不派生)
struct B {
virtual void f() const final; // do not override
virtual void g();
};
struct D : B {
void f() const; // error: D::f attempts to override final B::f
void g(); // OK
};
那么在这里使用final
关键字是否有意义?在我看来,你可以避免在这里使用virtual关键字,并且防止f()
被覆盖。
我不想敲它,因为上师可能会出现并提出一个用例,它解决了一些非常模糊的编译错误...但它看起来像一个愚蠢的事情。 – StoryTeller
[有没有将基类函数标记为virtual和final的意义?](https://stackoverflow.com/questions/16739135/is-there-any-sense-in-marking-a-base -class-function-as-both-virtual-and-final) –
实际上还有更多的重复,例如[最终虚拟函数有什么意义?](https://stackoverflow.com/questions/11704406/) – Ruslan