网站制作 合肥,有设计感的网站,网站建设项目考察范文,海安县住房和城乡建设局网站代理 代理支持任意参数的简单代理实现 代理
代理的本质是函数指针
代理分为单播#xff0c;多播#xff0c;动态多播#xff08;ue4中提出的#xff09;
单播#xff1a;在网络通信中#xff0c;单播是一种一对一的通信方式 多播#xff1a;在网络通信中#xff0c;… 代理 代理支持任意参数的简单代理实现 代理
代理的本质是函数指针
代理分为单播多播动态多播ue4中提出的
单播在网络通信中单播是一种一对一的通信方式 多播在网络通信中多播是一种一对多的通信方式
支持任意参数的简单代理实现 advanced.h #pragma once
#include iostream
#include type_traits
using namespace std;//工厂
templateclass T, class ...ParamTypes
T* CreateObject(ParamTypes ...Param)
{return new T(std::forwardParamTypes(Param)...);
}//void(*Funcation)(int a, int b, int c);//最简单的代理(支持多参数代理)
templateclass TObjectType,class TReturn,typename...ParamTypes//对象类型返回值可变参数
class FDelegate
{
public:FDelegate(TObjectType* InObject, TReturn(TObjectType::* InFuncation)(ParamTypes...))//使用代理时需要传一个对象因为通过对象调用:Object(InObject)//把对象进行初始化, Funcation(InFuncation)//函数指针初始化{}TReturn operator()(ParamTypes...Params)//重载操作符{//通过当前对象调用函数指针return (Object-*Funcation)(std::forwardParamTypes(Params)...);//完美转换之后把参数传递}
private:TObjectType *Object;TReturn (TObjectType::* Funcation)(ParamTypes...);//声明一个函数指针通过TObjectType::* Funcation指针调用ParamTypes...任意参
};//工厂
templateclass TObjectType,class TReturn,typename...ParamTypes
FDelegateTObjectType, TReturn, ParamTypes...CreateDelegate(TObjectType* InObject, TReturn(TObjectType::* InFuncation)(ParamTypes...))
{return FDelegateTObjectType, TReturn, ParamTypes...(InObject, InFuncation);//拷贝是指针的拷贝内容不会拷贝如果想拷贝内容需要进行深拷贝
}学习.cpp #include iostream
#includeadvanced.hstruct FTestA
{};struct FTestB
{FTestB(int a, int b){}int print(int a, int b){printf(%i %i, a, b);return a b;}
};int main()
{FTestA* p CreateObjectFTestA();FTestB* p2 CreateObjectFTestB(1,2);auto NewFunction CreateDelegate(p2, FTestB::print);int a NewFunction(1, 2);cout \n a endl;return 0;
}