C++类模板

创建类模板

头文件:

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();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值