我有两个DLL - Master.dll和Slave.dll。 在Master.dll中,我定义了一个模板类:
// MyClass.hpp
#include <vector>
#define EXPORT __declspec( dllexport )
template< int num, typename ... args >
class EXPORT MyClass {
private:
static MyClass* Instance;
public:
std::vector< void* > Callbacks;
static MyClass* GetInstance() {
if ( Instance == nullptr ) {
Instance = new MyClass();
}
return Instance;
}
};
// Initialize the static member
template< int num, typename ... args >
MyClass< num, args >* MyClass< num, args >::Instance;
在Slave.dll中,我使用模板类,如下所示:
// SlaveImpl.cpp
#include "MyClass.hpp"
#define EXPORT __declspec( dllimport )
void SomeFunction() {
auto inst = MyClass<1, int, float>::GetInstance();
inst->Callbacks.push_back( (void*)0 );
std::cout << inst << std::endl;
}
然后我尝试在Master.dll中再次访问它:
// SomeMasterFile.cpp
void SomeMasterFunction {
auto inst = MyClass<1, int, float>::GetInstance();
std::cout << inst << " - " << inst->Callbacks.size() << std::endl;
}
然后我比较两个控制台结果,地址不同,Callbacks
向量的大小为0:
4E142056
4F5262A1 - 0
如何让两个DLL使用相同的指针,以便我的Master.dll可以访问与Slave.dll相同的Callbacks
向量?