创建类模板
头文件:
template <typename T>
class A: public T
{
public:
double test() const;
};
template <typename T>
double A<T>::test() const
{
return T::aa;
}
namespace T1
{
struct T2
{
static const double aa;
};
const double T2::aa = 1;
}
CPP文件:
int main()
{
A<::T1::T2> a;
double b = a.test();
return 0;
}
在DLL里通过函数将类传出
之前的头文件做如下的修改:
class AInterface
{
public:
virtual double test() const = 0;
};
template <typename T>
class A: public T, public AInterface
{
public:
A();
double test() const;
};
template <typename T>
A<T>::A()
{}
extern "C" __declspec(dllexport) AInterface* getA();
cpp:
#include "header.h"
extern "C" __declspec(dllexport) AInterface* getA()
{
return new A<::T1::T2>();
}
调用:
#include <Windows.h>
#include "../TemplateClassDll/header.h" //需要include头文件
typedef AInterface* (*getFunction)();
int main()
{
HMODULE module = LoadLibrary("..//Debug//TemplateClassDll.dll");
getFunction fun = reinterpret_cast<getFunction>(GetProcAddress(module, "getA"));
double a = fun()->test();
}