【发布时间】:2012-11-04 21:57:41
【问题描述】:
我有一个主应用程序,它有一个接口(抽象类),这个接口需要在主应用程序和外部 dll 中都有实现。 我将使用指向此接口的指针来访问方法,因此我将根据某些条件将指针分配给任何一个实现的地址。
如何做到这一点?
我在堆栈溢出中遇到了一个question,其中标记为解决方案的答案说
主应用中的界面
class IModule
{
public:
virtual ~IModule(); // <= important!
virtual void doStuff() = 0;
};
可以在主应用中实现
class ActualModule: public IModule
{
/* implementation */
};
并且可以从dll中导出一个函数来返回指向dll中实现的指针
__declspec (dllexport) IModule* CreateModule()
{
// call the constructor of the actual implementation
IModule * module = new ActualModule();
// return the created function
return module;
}
dll如何知道IModule之类的东西存在?
我可以将 IModule 标记为 extern 并在 dll 中使用吗?
【问题讨论】:
标签: c++ dll interface implementation