【发布时间】:2015-07-30 14:50:53
【问题描述】:
当 final 关键字被添加到虚函数声明中时,我遇到了一个奇怪的情况,它的定义在单独的 .cpp 文件中。
考虑以下示例:
IClass.hpp
class IClass //COM-like base interface
{
protected:
virtual ~IClass(){} //derived classes override this
public:
virtual void release() final;
};
dllmain.cpp(共享库)
#include "IClass.hpp"
...
void IClass::release()
{
delete this;
}
...
main.cpp(独立可执行文件)
//various includes here
...
int main(int argc, char** argv)
{
/* From "IGameEngine.hpp"
class IGameEngine : public IClass
{
...
};
*/
IGameEngine* engine = factoryGameEngine();
...
engine->release();
return 0;
}
事实上,GCC 4.9.2 将报告undefined reference to 'IClass::release()'
我的目标是让IClass::release() 不可覆盖,同时将其实现隐藏在游戏引擎的共享库中。
有什么建议吗?
【问题讨论】:
-
虚拟函数总是被 odr-used 除非它们是纯的。我相信在这种情况下允许链接器发出错误。
-
我无法在我的 gcc 4.9.2 上重现该问题。请添加您构建程序的方式(编译器选项等)
-
你从dll中导出函数了吗? @0x49 我错觉他们的 odr-used-ness 是实现定义的。
-
@dyp: dll 是使用这个makefile 构建的 主二进制文件使用相同的编译器标志,但以下链接器标志:
-static-libgcc -static-libstdc++ -mwindows -
@Yakk [basic.def.odr]/p5:“如果虚拟成员函数不是纯的,则它是 odr-used。”我认为是否存在链接器错误取决于实现。
标签: c++ final c++14 undefined-reference