首页 > 其他 > 详细

Delegate

时间:2016-03-26 23:45:56      阅读:281      评论:0      收藏:0      [点我收藏+]

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;
}







Delegate

原文:http://www.cnblogs.com/freezlz/p/5324254.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!