我讀一個真棒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