【发布时间】:2020-10-19 06:25:40
【问题描述】:
我正在创建一个 DLL API (MSVC12),我真的很想使用类导出。
问题是:我有一个嵌套类,它的调用被处理为“直接调用”,
我没有找到一种强制虚拟桌调用的优雅方式。
(注意:为了便于阅读,我修剪了一些界面)
// interface.h
class CEvent {
public:
virtual bool hasOccured();
}
class ISimulator {
public:
virtual void Init();
CEvent initEnded;
}
void DLL_API_INTERFACE ISimulator* GetSimulator();
// interface.c - DLL implementations -> Irrelevant
// CLIENT APP
auto mdl = LoadLibrary(...);
auto getSimFunc = GetProcAddress(mdl, ...);
ISimulator* my_sim = getSimFunc();
m_sim->Init(); // << OK
...
...
m_sim->initEnded.hasOccured() // << LNK: Undefined Reference
用 CEvent* 代替 CEvent 的明显解决方案似乎很糟糕,因为它需要堆分配和适当的破坏,我倾向于尽可能避免。
另一种解决方案是对CEvent 进行访问,该访问器返回CEvent*,从而强制进行vtbl 调用。
我的问题:我可以强制编译器将CEvent::hasOccured() 视为虚拟调用吗?
【问题讨论】:
-
请提供minimal reproducible example 完整的源代码和完整的错误信息。不过,看起来您并没有导出类