Delegate
2014-06-29 星期日 19:36:45 
委托简单点说,就是委托者把其要委托的功能以pfunc的形式传递给受托者,由受托者来调用pfunc。
代码来自网络,稍加修改。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | //delegate//受托者//委托者以模板的方式出现template<typename T>class Cconsignee{private: typedef int (T::*delegateFun)(int); T * _This; delegateFun _deleGate;public: //This被代理的对象, delegateFun被代理的方法 Cconsignee(T * This, int (T::*delegateFun)(int)) { _This = This; _deleGate = delegateFun; } //c被代理的参数 int execue(int c) { return (_This->*_deleGate)(c); }};//委托者class CDelegator{public: int Fun0(int a) {return a + 10;} int Fun1(int a) {return a - 10;} CDelegator() { }};int main(int argc, char* argv[]){ CDelegator *pDelegator = new CDelegator(); Cconsignee<CDelegator> t_consignee1(pDelegator, (&CDelegator::Fun0)); Cconsignee<CDelegator> t_consignee2(pDelegator, (&CDelegator::Fun1)); cout << t_consignee1.execue(10) <<endl; cout << t_consignee2.execue(20) <<endl; return 0;} |

浙公网安备 33010602011771号