作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
如果我有一个程序 foo.exe 并且它在运行时加载了一个插件 bar.dll,我如何让 bar.dll 中的代码使用 foo.exe 中的类? bar.dll 是否可以从 foo.exe 派生类?另外,该解决方案是否是跨平台的?
编辑:这是我正在尝试做的更多事情:
//foo.exe
class Node {
public:
void SetName(const string& n) { ... }
const string& GetName() { ... }
};
//bar.dll
class TransformNode : public Node {
public:
void DoSomething() {
SetName(...); //basically, can I inherit functionality from foo.exe (SetName and GetName)
//and reuse the code in a derived class in bar.dll?
}
};
最佳答案
为了让 bar.dll 使用来自 foo.exe 的类,您需要将一个带有虚函数的类的实例从 foo.exe 传递给 bar.dll。 Bar.dll 将调用来自 foo.exe 的虚函数。
是的,bar.dll 可以将派生类传递回 foo.exe,然后让 foo.exe 调用所述类的虚函数。
此解决方案将是跨平台的,但您将遇到重大问题,以确保您不会破坏共享库和 exe 之间的 abi。即使是最简单的更改也很容易破坏 abi。
This很好地介绍了可以破坏 abi 的因素。
关于c++ - 您如何向插件库公开功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8622854/
我是一名优秀的程序员,十分优秀!