C++ warning:’xxx‘ has no out-of-line virtual method definitions

現象:在類中定義了虛函數並且直接在類定義內部實現這些虛函數時,編譯器就會報警告:’xxx‘ has no out-of-line virtual method definitions;its vtable will be emitted in every translation unit.

解決方法:

If a class is defined in a header file and has a vtable (either it has virtual methods or it derives from classes with virtual methods), it must always have at least one out-of-line virtual method in the class. Without this, the compiler will copy the vtable and RTTI into every .o file that #includes the header, bloating .o file sizes and increasing link times

這裏大概意思就是:
如果在頭文件中定義了一個類並且具有vtable(它具有虛方法或者它來自具有虛方法的類),則它必須始終在類中具有至少一個外聯虛擬方法 如果沒有這個,編譯器會將vtable和RTTI複製到每個.o文件中,其中#include標題,膨脹.o文件大小和增加鏈接時間。
因爲我是在類中定義了虛函數並且直接在類定義內部實現這些虛函數,所以將虛函數自動變成了內聯函數,那麼只需要把虛函數的實現挪到類的外面去實現就可以啦。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章