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文件大小和增加链接时间。
因为我是在类中定义了虚函数并且直接在类定义内部实现这些虚函数,所以将虚函数自动变成了内联函数,那么只需要把虚函数的实现挪到类的外面去实现就可以啦。

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