搜索下载文件: fastdelegate.h
下载地址:
[url]http://code.google.com/p/code-adintr-com/source/browse/trunk/mylib/aslib/utility/FastDelegate.h?r=4[/url]
下载地址:
[url]http://code.google.com/p/code-adintr-com/source/browse/trunk/mylib/aslib/utility/FastDelegate.h?r=4[/url]
#include <stdio.h>
#include "fastdelegate.h"
///
//被委托绑定的类成员函数
class Calculator
{
public:
int Add(int nA, int nB)
{
return nA + nB;
}
int Sub(int nA, int nB)
{
return nA - nB;
}
int Multi(int nA, int nB)
{
return nA * nB;
}
};
/
//定义函数指针
typedef fastdelegate::FastDelegate2<int, int, int> AddFunc;
typedef fastdelegate::FastDelegate2<int, int, int> SubFunc;
typedef fastdelegate::FastDelegate2<int, int, int> MultiFunc;
//实现绑定到处理类
class CalcHander
{
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 Param2, class RetType>
void bind_sub(Y* x, RetType (X::*func)(Param1 p1, Param2 p2))
{
pSubFunc = fastdelegate::MakeDelegate(x, func);
}
template <class X, class Y, class Param1, class Param2, class RetType>
void bind_Multi(Y* x, RetType (X::*func)(Param1 p1, Param2 p2))
{
pMultiFunc = fastdelegate::MakeDelegate(x, func);
}
public:
void Init(Calculator *pCalculator)
{
bind_add(pCalculator, &Calculator::Add);
bind_sub(pCalculator, &Calculator::Sub);
bind_Multi(pCalculator, &Calculator::Multi);
}
void ExecuteAdd(int nA, int nB)
{
int nSum = pAddFunc(nA, nB);
printf("Sum=%d\n", nSum);
}
void ExecuteSub(int nA, int nB)
{
int nSub = pSubFunc(nA, nB);
printf("Sub=%d\n", nSub);
}
void ExecuteMulti(int nA, int nB)
{
int nMulti = pMultiFunc(nA, nB);
printf("Multi=%d\n", nMulti);
}
private:
AddFunc pAddFunc;
SubFunc pSubFunc;
MultiFunc pMultiFunc;
};
int main(int argc, char* argv[])
{
Calculator calculator; //被绑定对象声明
CalcHander calcHander; //主动绑定的对象声明
calcHander.Init(&calculator); //函数绑定
//执行函数,调用绑定函数
calcHander.ExecuteAdd(200, 100); //300 相当于调用 calculator.Add(200, 100);
calcHander.ExecuteSub(300, 100); //200 相当于调用 calculator.Sub(300, 100);
calcHander.ExecuteMulti(50, 20); //1000 相当于调用 calculator.Multi(50, 20);
return 0;
}