C++ 委托 fastdelegate
2017-07-20 14:00
323 查看
fastdelegate 是c++实现的快速委托机制,
其本质就是用来执行方法(函数)的一个东西。
#include <stdio.h>
#include "fastdelegate.h"
typedef fastdelegate::FastDelegate2<int, int, int> AddFunc;
typedef fastdelegate::FastDelegate1<string> DoSomeThingFunc;
class Demo
{
public:
int Add(int a, int b)
{
return a + b;
}
int DoSomeThing (string &str)
{
//do some thing
}
};
class HandleDemo
{
public:
template <class X, class Y, class Param1, class Param2, class RetType>
void bind_add(Y* x, RetType (X::*func)(Param1 p1, Param2 p2))
{
pAddFunc = fastdelegate::MakeDelegate(x, func);
}
template <class X, class Y, class Param1, class RetType>
void bind_do(Y* x, RetType (X::*func)(Param1 p1))
{
pDoSomeThingFunc = fastdelegate::MakeDelegate(x, func);
}
public:
void Init(Demo *pDemo)
{
bind_add(pDemo, &Demo::Add);
bind_do(pdemo, &Demo::DoSomeThing );
}
void ExecuteAdd(int a, int b)
{
int Sum = pAddFunc(a, b);
printf("Sum=%d\n", Sum);
}
void ExecuteDo(String str)
{
pDoSomeThingFunc (str);
}
private:
AddFunc
pAddFunc;
DoSomeThingFunc pDoSomeThingFunc ;
};
int main(int argc, char* argv[])
{
Demo demo;
HandleDemo handldemo;
handldemo.Init(&demo);
//执行函数,调用绑定函数
handldemo.ExecuteAdd(200, 100);
handldemo.ExecuteDo("hello!");
return 0;
}
相当于demo 委托handledemo来执行它的功能函数,ExecuteAdd 和ExecuteDo实际执行的是Add ,DoSomeThing
其本质就是用来执行方法(函数)的一个东西。
#include <stdio.h>
#include "fastdelegate.h"
typedef fastdelegate::FastDelegate2<int, int, int> AddFunc;
typedef fastdelegate::FastDelegate1<string> DoSomeThingFunc;
class Demo
{
public:
int Add(int a, int b)
{
return a + b;
}
int DoSomeThing (string &str)
{
//do some thing
}
};
class HandleDemo
{
public:
template <class X, class Y, class Param1, class Param2, class RetType>
void bind_add(Y* x, RetType (X::*func)(Param1 p1, Param2 p2))
{
pAddFunc = fastdelegate::MakeDelegate(x, func);
}
template <class X, class Y, class Param1, class RetType>
void bind_do(Y* x, RetType (X::*func)(Param1 p1))
{
pDoSomeThingFunc = fastdelegate::MakeDelegate(x, func);
}
public:
void Init(Demo *pDemo)
{
bind_add(pDemo, &Demo::Add);
bind_do(pdemo, &Demo::DoSomeThing );
}
void ExecuteAdd(int a, int b)
{
int Sum = pAddFunc(a, b);
printf("Sum=%d\n", Sum);
}
void ExecuteDo(String str)
{
pDoSomeThingFunc (str);
}
private:
AddFunc
pAddFunc;
DoSomeThingFunc pDoSomeThingFunc ;
};
int main(int argc, char* argv[])
{
Demo demo;
HandleDemo handldemo;
handldemo.Init(&demo);
//执行函数,调用绑定函数
handldemo.ExecuteAdd(200, 100);
handldemo.ExecuteDo("hello!");
return 0;
}
相当于demo 委托handledemo来执行它的功能函数,ExecuteAdd 和ExecuteDo实际执行的是Add ,DoSomeThing
相关文章推荐
- C++ 委托 fastdelegate使用,实现类函数回调绑定
- c++高效委托的实现(fast delegate---简单、高效、跨平台)
- c++ fast Delegate 实现代码
- 成员函数指针与高效C++委托 (delegate)
- 事件处理(Handling Events)和委托(Delegate)代码示例(二)【UE4】【C++】
- C++中实现委托(Delegate)
- 【转载】C++中实现委托(Delegate)
- 在C++中实现委托(Delegate)
- 使用蓝图(Blueprint)绑定多播委托(Multicast Delegate)【UE4】【C++】
- 用 C++ 实现 C# 中的 委托/事件 (1-delegateEMU.cpp)
- 用 C++ 实现 C# 中的 委托/事件 (2-delegate event functor)
- Fast C++ Delegate: Boost.Function 'drop-in' replacement and multicast
- C#不是C++ 之 委托(Delegate)
- C++ FastDelegate 扩展,实现与.net类似的事件处理功能
- 事件处理(Handling Events)和委托(Delegate)代码示例(三)【UE4】【C++】
- 在C++中实现委托(Delegate)
- Fast C++ Delegate: Boost.Function 'drop-in' replacement and multicast
- C#不是C++ 之 委托(Delegate)
- 成员函数指针与高效C++委托 (delegate)
- 事件处理(Handling Events)和委托(Delegate)代码示例(一)【UE4】【C++】