希望想理解C++委托的同学,能够从代码中悟出其中的原理。有什么不太清楚的地方,欢迎留言交流。

 #include <bits/stdc++.h>
using namespace std; #define debug(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl class AF
{
public:
AF() {}
void Func(int x){
cout << "AF::Func " << x << endl;
}
}; void (AF::*pFunc)(int) = &AF::Func; int main(int argc, char *argv[])
{
AF af;
(af.*pFunc)();
AF *paf = &af;
(paf->*pFunc)(); return ;
}

成员函数指针的操作

 #include <bits/stdc++.h>
using namespace std; #define debug(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl class AF
{
public:
AF() {}
void Func(int x){
cout << "AF::Func " << x << endl;
}
}; void (AF::*pFunc)(int) = &AF::Func; class BF
{
public:
BF() {}
void method(int x){
cout << "BF::method " << x << endl;
}
}; /* gcc-4.4.7 不支持特化与偏特化模板
template <typename N>
class Test<char, N>
{
public:
Test( char i, N j ):a( i ), b( j )
{
cout<<"模版类偏特化"<< a<< ' ' << b << endl;
}
private:
char a;
N b;
};
*/ /*
template<typename T>
class DelegateNoneMemFunc
{
void (*m_pFunc)(int);
public:
DelegateNoneMemFunc(void (*pFunc)(int) ):m_pFunc(pFunc) {}
void invoke(int value){
(*m_pFunc)(value);
}
};
*/ void NonmemberFunc(int value){
printf("NonmemberFunc: %d\n", value);
} class IDelegateHandler
{
public:
virtual ~IDelegateHandler() {}
virtual void invoke(int) = ;
}; /*
template <typename T>
class DelegateHandlerChild : public IDelegateHandler
{
public:
DelegateHandlerChild() {}
void invoke(int value){
printf("DelegateHandlerChild invoke: %d", value);
}
};
*/ //*
template <typename T>
class DelegateHandler
{
T *m_pT;
void (T::*m_pFunc)(int);
public:
DelegateHandler(T *pT, void (T::*pFunc)(int)):m_pT(pT), m_pFunc(pFunc) {} void invoke(int value){
(m_pT->*m_pFunc)(value);
}
}; template <typename T>
class DelegateNmemHandler
{
void (*m_pFunc)(int);
public:
DelegateNmemHandler(void (*pFunc)(int)):m_pFunc(pFunc) {} void invoke(int value){
(*m_pFunc)(value);
}
}; int main(int argc, char *argv[])
{
AF af;
BF bf; (af.*pFunc)();
AF *paf = &af;
(paf->*pFunc)(); // template
DelegateHandler<AF> afT(&af, &AF::Func);
afT.invoke(); DelegateHandler<BF> bfT(&bf, &BF::method);
bfT.invoke(); // 非成员函数
DelegateNmemHandler<void> nMemFunc(NonmemberFunc);
nMemFunc.invoke(); return ;
} class LuggageCompartment {
private:
int m_iLuggage; //私有变量,保存现在的行李总数
public:LuggageCompartment() {
m_iLuggage = ;
} //构造函数
int TakeoutLuggage() //取出一件行李
{
if (m_iLuggage != )
m_iLuggage--;
return m_iLuggage;
}
int InsertLuggage() //放入一件行李
{
return (++m_iLuggage);
}
int checkLuggage() //检查行李总数
{
return m_iLuggage;
}
}; class FlightSegment {
private:LuggageCompartment * m_pLC; //成员指针
public:void SetLuggageCompartment(LuggageCompartment * pLC) {
m_pLC = pLC;
} //设置成员指针
FlightSegment() //构造函数将成员指针初始化为null
{
m_pLC = NULL;
}
int checkLuggage() {
if (m_pLC == NULL)
return -;
return m_pLC->checkLuggage(); //将函数调用委托给成员指针
}
};

使用类模板

 #include <bits/stdc++.h>
using namespace std; #define debug(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl class AF
{
public:
AF() {}
void Func(int x){
cout << "AF::Func " << x << endl;
}
}; void (AF::*pFunc)(int) = &AF::Func; class BF
{
public:
BF() {}
void method(int x){
cout << "BF::method " << x << endl;
}
}; /* gcc-4.4.7 不支持特化与偏特化模板
template <typename N>
class Test<char, N>
{
public:
Test( char i, N j ):a( i ), b( j )
{
cout<<"模版类偏特化"<< a<< ' ' << b << endl;
}
private:
char a;
N b;
};
*/ void NonmemberFunc(int value){
printf("NonmemberFunc: %d\n", value);
} template <typename T>
class DelegateHandler
{
T *m_pT;
void (T::*m_pFunc)(int);
public:
DelegateHandler(T *pT, void (T::*pFunc)(int)):m_pT(pT), m_pFunc(pFunc) {} void invoke(int value){
(m_pT->*m_pFunc)(value);
}
}; // 非成员函数
template <typename T>
class DelegateNmemHandler
{
void (*m_pFunc)(int);
public:
DelegateNmemHandler(void (*pFunc)(int)):m_pFunc(pFunc) {} void invoke(int value){
(*m_pFunc)(value);
}
}; class IDelegateHandler
{
public:
virtual ~IDelegateHandler() {}
virtual void invoke(int) = ;
}; template <typename T>
class DelegateHandlerChild : public IDelegateHandler
{
T *m_pT;
void (T::*m_pFunc)(int);
public:
DelegateHandlerChild(T *pT, void (T::*pFunc)(int)):m_pT(pT), m_pFunc(pFunc) {} void invoke(int value){
(m_pT->*m_pFunc)(value);
}
}; template <typename T>
class DelegateNmemHandlerChild : public IDelegateHandler
{
void (*m_pFunc)(int);
public:
DelegateNmemHandlerChild(void (*pFunc)(int)):m_pFunc(pFunc) {} void invoke(int value){
(*m_pFunc)(value);
}
}; int main(int argc, char *argv[])
{
AF af;
BF bf;
/*
(af.*pFunc)(10);
AF *paf = &af;
(paf->*pFunc)(11); // template
DelegateHandler<AF> afT(&af, &AF::Func);
afT.invoke(3); DelegateHandler<BF> bfT(&bf, &BF::method);
bfT.invoke(4); DelegateNmemHandler<void> nMemFunc(NonmemberFunc);
nMemFunc.invoke(5);
*/
DelegateHandlerChild<AF> ac(&af, &AF::Func);
DelegateHandlerChild<BF> bc(&bf, &BF::method);
DelegateNmemHandlerChild<void> vc(NonmemberFunc); vector<IDelegateHandler *> handler;
handler.push_back(&ac);
handler.push_back(&bc);
handler.push_back(&vc); for(auto iter = handler.begin(); iter != handler.end(); iter++)
(*iter)->invoke();
return ;
}

使用多态

 #include <bits/stdc++.h>
using namespace std; #define debug(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl class AF
{
public:
AF() {}
void Func(int x){
cout << "AF::Func " << x << endl;
}
}; void (AF::*pFunc)(int) = &AF::Func; class BF
{
public:
BF() {}
void method(int x){
cout << "BF::method " << x << endl;
}
}; /* gcc-4.4.7 不支持特化与偏特化模板
template <typename N>
class Test<char, N>
{
public:
Test( char i, N j ):a( i ), b( j )
{
cout<<"模版类偏特化"<< a<< ' ' << b << endl;
}
private:
char a;
N b;
};
*/ void NonmemberFunc(int value){
printf("NonmemberFunc: %d\n", value);
} /*
template <typename T>
class DelegateHandler
{
T *m_pT;
void (T::*m_pFunc)(int);
public:
DelegateHandler(T *pT, void (T::*pFunc)(int)):m_pT(pT), m_pFunc(pFunc) {} void invoke(int value){
(m_pT->*m_pFunc)(value);
}
}; // 非成员函数
template <typename T>
class DelegateNmemHandler
{
void (*m_pFunc)(int);
public:
DelegateNmemHandler(void (*pFunc)(int)):m_pFunc(pFunc) {} void invoke(int value){
(*m_pFunc)(value);
}
}; // 多态
class IDelegateHandler
{
public:
virtual ~IDelegateHandler() {}
virtual void invoke(int) = 0;
}; template <typename T>
class DelegateHandlerChild : public IDelegateHandler
{
T *m_pT;
void (T::*m_pFunc)(int);
public:
DelegateHandlerChild(T *pT, void (T::*pFunc)(int)):m_pT(pT), m_pFunc(pFunc) {} void invoke(int value){
(m_pT->*m_pFunc)(value);
}
}; template <typename T>
class DelegateNmemHandlerChild : public IDelegateHandler
{
void (*m_pFunc)(int);
public:
DelegateNmemHandlerChild(void (*pFunc)(int)):m_pFunc(pFunc) {} void invoke(int value){
(*m_pFunc)(value);
}
}; */
// 使用宏
#define DECLARE_PARAMS(...) __VA_ARGS__
#define DECLARE_ARGS(...) __VA_ARGS__ //0个参数的委托
#define DELEGATE0(retType, name) \
DECLARE_DELEGATE(retType, name, DECLARE_PARAMS(void), ) //1个参数的委托
#define DELEGATE1(retType, name, p1) \
DECLARE_DELEGATE( \
retType, \
name, \
DECLARE_PARAMS(p1 a), \
DECLARE_ARGS(a)) //2个参数的委托
#define DELEGATE2(retType, name, p1, p2) \
DECLARE_DELEGATE( \
retType, \
name, \
DECLARE_PARAMS(p1 a, p2 b), \
DECLARE_ARGS(a, b)) //3个参数的委托
#define DELEGATE3(retType, name, p1, p2, p3) \
DECLARE_DELEGATE( \
retType, \
name, \
DECLARE_PARAMS(p1 a, p2 b, p3 c), \
DECLARE_ARGS(a, b, c)) //4个参数的委托
#define DELEGATE4(retType, name, p1, p2, p3, p4) \
DECLARE_DELEGATE( \
retType, \
name, \
DECLARE_PARAMS(p1 a, p2 b, p3 c, p4 d), \
DECLARE_ARGS(a, b, c, d)) //5个参数的委托
#define DELEGATE5(retType, name, p1, p2, p3, p4, p5) \
DECLARE_DELEGATE( \
retType, \
name, \
DECLARE_PARAMS(p1 a, p2 b, p3 c, p4 d, p5 e), \
DECLARE_ARGS(a, b, c, d, e)) //6个参数的委托
#define DELEGATE6(retType, name, p1, p2, p3, p4, p5, p6) \
DECLARE_DELEGATE( \
retType, \
name, \
DECLARE_PARAMS(p1 a, p2 b, p3 c, p4 d, p5 e, p6 f), \
DECLARE_ARGS(a, b, c, d, e, f)) //7个参数的委托
#define DELEGATE7(retType, name, p1, p2, p3, p4, p5, p6, p7) \
DECLARE_DELEGATE( \
retType, \
name, \
DECLARE_PARAMS(p1 a, p2 b, p3 c, p4 d, p5 e, p6 f, p7 g), \
DECLARE_ARGS(a, b, c, d, e, f, g)) //8个参数的委托
#define DELEGATE8(retType, name, p1, p2, p3, p4, p5, p6, p7, p8) \
DECLARE_DELEGATE( \
retType, \
name, \
DECLARE_PARAMS(p1 a, p2 b, p3 c, p4 d, p5 e, p6 f, p7 g, p8 h), \
DECLARE_ARGS(a, b, c, d, e, f, g, h)) #define DECLARE_DELEGATE(retType, name, params, args) \
class I##name { \
public: \
virtual ~I##name() { } \
virtual retType Invoke(params) = ; \
}; \
template<typename T> \
class name : public I##name { \
public: \
name(T* pType, retType (T::*pFunc)(params)) \
: m_pType(pType), m_pFunc(pFunc) { } \
retType Invoke(params) { \
return (m_pType->*m_pFunc)(args); \
} \
private: \
T* m_pType; retType (T::*m_pFunc)(params); \
}; \
template<> \
class name<void> : public I##name { \
public: \
name(retType (*pFunc)(params)) \
: m_pFunc(pFunc) { } \
retType Invoke(params) { \
return (*m_pFunc)(args); \
} \
private: \
retType (*m_pFunc)(params); \
} DELEGATE0(void, Test0);
DELEGATE1(void, Test1, int);
DELEGATE2(void, Test2, int, int); void print(){
printf("Just 0 arg test\n");
} int main(int argc, char *argv[])
{
AF af;
BF bf;
/*
(af.*pFunc)(10);
AF *paf = &af;
(paf->*pFunc)(11); // template
DelegateHandler<AF> afT(&af, &AF::Func);
afT.invoke(3); DelegateHandler<BF> bfT(&bf, &BF::method);
bfT.invoke(4); DelegateNmemHandler<void> nMemFunc(NonmemberFunc);
nMemFunc.invoke(5);
*/
/*
DelegateHandlerChild<AF> ac(&af, &AF::Func);
DelegateHandlerChild<BF> bc(&bf, &BF::method);
DelegateNmemHandlerChild<void> vc(NonmemberFunc); vector<IDelegateHandler *> handler;
handler.push_back(&ac);
handler.push_back(&bc);
handler.push_back(&vc); for(auto iter = handler.begin(); iter != handler.end(); iter++)
(*iter)->invoke(8);
*/ Test0<void> t0(print);
t0.Invoke(); Test1<AF> test(&af, &AF::Func);
test.Invoke(); Test1<void> tet(NonmemberFunc);
tet.Invoke(); return ;
}

使用宏定义

C++委托模式的更多相关文章

  1. 用java语言实现事件委托模式

    http://blog.csdn.net/yanshujun/article/details/6494447 用java语言实现事件委托模式 2010-04-27 00:04 2206人阅读 评论(1 ...

  2. iOS中常见的设计模式——单例模式\委托模式\观察者模式\MVC模式

    一.单例模式 1. 什么是单例模式? 在iOS应用的生命周期中,某个类只有一个实例. 2. 单例模式解决了什么问题? 想象一下,如果我们要读取文件配置信息,那么每次要读取,我们就要创建一个文件实例,然 ...

  3. ios专题 - 委托模式实现

    在ios中,委托模式非常常见,那委托模式是什么? 委托模式是把一个对象把请求给另一个对象处理. 下面见例子: #import <UIKit/UIKit.h> @protocol LQIPe ...

  4. 设计模式--委托模式C++实现

    原文章地址:http://www.cnblogs.com/zplutor/archive/2011/09/17/2179756.html [委托模式 C++实现] 我对.Net的委托模型印象很深刻,使 ...

  5. PHP设计模式之委托模式

    委托模式: 通过分配或委托至其他对象,委托设计模式能够去除核心对象中的判决和复杂的功能性. class Bank{ protected $info; /* 设置基本信息 @param string $ ...

  6. IOS常用设计模式之委托模式

    对于iOS开发,举例Cocoa框架下的几个设计模式为大家分析.当然,Cocoa框架下关于设计模式的内容远远不止这些,我们选择了常用的几种:单例模式.委托模式.观察者模式.MVC模式. 委托模式 委托模 ...

  7. classloader加载的双亲委托模式

    要深入了解ClassLoader,首先就要知道ClassLoader是用来干什么的,顾名思义,它就是用来加载Class文件到JVM,以供程序使用 的.我们知道,java程序可以动态加载类定义,而这个动 ...

  8. [js高手之路]设计模式系列课程-委托模式实战微博发布功能

    在实际开发中,经常需要为Dom元素绑定事件,如果页面上有4个li元素,点击对应的li,弹出对应的li内容,怎么做呢?是不是很简单? 大多数人的做法都是:获取元素,绑定事件 <ul> < ...

  9. 再起航,我的学习笔记之JavaScript设计模式28(委托模式)

    ## 委托模式 ### 概念介绍 **委托模式(Entrust): **多个对象接收并处理同一请求,他们将请求委托给另一个对象统一处理请求. ### 利用委托优化循环 如果我们有一个需求需要让用户点击 ...

  10. Java类加载双亲委托模式优点

    启动类加载器可以抢在标准扩展类加载器之前去装载类,而标准扩展类装载器可以抢在类路径加载器之前去加载那个类,类路径装载器又可以抢在自定义类装载器之前去加载类.所以Java虚拟机先从最可信的Java核心A ...

随机推荐

  1. Windows下TeX Live + Sublime Text 3 + Sumatra PDF配置

    本文写给我的师弟们,如何自己动手配置LaTeX环境(通过LeX Live + Sublime Text 3 + Sumatra PDF). 1.TeX Live 配置 首先从TeX Live 下载IS ...

  2. Hadoop日记Day11---主从节点接口分析

    一.NameNode 的接口分析 1. NameNode本质 经过前面的学习,可以知道NameNode 本身就是一个java 进程.观察RPC.getServer()方法的第一个参数,发现是this, ...

  3. CS190.1x-ML_lab1_review_student

    这是CS190.1x第一次作业,主要教你如何使用numpy.numpy可以说是python科学计算的基础包了,用途非常广泛.相关ipynb文件见我github. 这次作业主要分成5个部分,分别是:数学 ...

  4. 【转】浅谈php://filter的妙用

    php://filter是PHP中独有的协议,利用这个协议可以创造很多“妙用”,本文说几个有意思的点,剩下的大家自己下去体会. XXE中的使用 php://filter之前最常出镜的地方是XXE.由于 ...

  5. LintCode——旋转字符串

    描述:给定一个字符串和一个偏移量,根据偏移量旋转字符串(从左向右旋转) 样例:对于字符串 "abcdefg"     offset=0 => "abcdefg&qu ...

  6. pip virtualenv requirement

    原文:https://blog.csdn.net/u011860731/article/details/46561513 pip可以很方便的安装.卸载和管理Python的包.virtualenv则可以 ...

  7. nginx反向代理tomcat应用,struts2网站程序redirect时导致请求地址错误的解决方法

    一个使用struts2的网站在登录页面需要进行redirect跳转,大致如下: <package name="admin" extends="httl-defaul ...

  8. error: Build input file cannot be found: '*******/node_modules/react-native/Libraries/WebSocket/libfishhook.a' 问题解决记录

    解决了刚才的'config.h' file not found问题,本以为就可以顺畅的跑起来,谁知道又被恶心到了,Build input file cannot be found!!! 问题: err ...

  9. PAT甲题题解-1049. Counting Ones-数学问题

    n位数,总共有0~10^n-1共计10^n个数那么所有数出现的总次数变为n*(10^n)个数1出现的次数便是十分之一,所以n位数中,1出现的次数为n*10^(n-1)知道这一个后,接下来就方便求了. ...

  10. JSP JSTL知识结构图

    自行绘制,欢迎指正.