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

我只是一个虾纸丫 提交于 2020-01-19 19:32:04

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!