C++委托模式
希望想理解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 = ⁡
(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 = ⁡
(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 = ⁡
(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 = ⁡
(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++委托模式的更多相关文章
- 用java语言实现事件委托模式
		
http://blog.csdn.net/yanshujun/article/details/6494447 用java语言实现事件委托模式 2010-04-27 00:04 2206人阅读 评论(1 ...
 - iOS中常见的设计模式——单例模式\委托模式\观察者模式\MVC模式
		
一.单例模式 1. 什么是单例模式? 在iOS应用的生命周期中,某个类只有一个实例. 2. 单例模式解决了什么问题? 想象一下,如果我们要读取文件配置信息,那么每次要读取,我们就要创建一个文件实例,然 ...
 - ios专题 - 委托模式实现
		
在ios中,委托模式非常常见,那委托模式是什么? 委托模式是把一个对象把请求给另一个对象处理. 下面见例子: #import <UIKit/UIKit.h> @protocol LQIPe ...
 - 设计模式--委托模式C++实现
		
原文章地址:http://www.cnblogs.com/zplutor/archive/2011/09/17/2179756.html [委托模式 C++实现] 我对.Net的委托模型印象很深刻,使 ...
 - PHP设计模式之委托模式
		
委托模式: 通过分配或委托至其他对象,委托设计模式能够去除核心对象中的判决和复杂的功能性. class Bank{ protected $info; /* 设置基本信息 @param string $ ...
 - IOS常用设计模式之委托模式
		
对于iOS开发,举例Cocoa框架下的几个设计模式为大家分析.当然,Cocoa框架下关于设计模式的内容远远不止这些,我们选择了常用的几种:单例模式.委托模式.观察者模式.MVC模式. 委托模式 委托模 ...
 - classloader加载的双亲委托模式
		
要深入了解ClassLoader,首先就要知道ClassLoader是用来干什么的,顾名思义,它就是用来加载Class文件到JVM,以供程序使用 的.我们知道,java程序可以动态加载类定义,而这个动 ...
 - [js高手之路]设计模式系列课程-委托模式实战微博发布功能
		
在实际开发中,经常需要为Dom元素绑定事件,如果页面上有4个li元素,点击对应的li,弹出对应的li内容,怎么做呢?是不是很简单? 大多数人的做法都是:获取元素,绑定事件 <ul> < ...
 - 再起航,我的学习笔记之JavaScript设计模式28(委托模式)
		
## 委托模式 ### 概念介绍 **委托模式(Entrust): **多个对象接收并处理同一请求,他们将请求委托给另一个对象统一处理请求. ### 利用委托优化循环 如果我们有一个需求需要让用户点击 ...
 - Java类加载双亲委托模式优点
		
启动类加载器可以抢在标准扩展类加载器之前去装载类,而标准扩展类装载器可以抢在类路径加载器之前去加载那个类,类路径装载器又可以抢在自定义类装载器之前去加载类.所以Java虚拟机先从最可信的Java核心A ...
 
随机推荐
- 【来龙去脉系列】AutoMapper一款自动映射框架
			
前言 通常在一个应用程序中,我们开发人员会在两个不同的类型对象之间传输数据,通常我们会用DTOs(数据传输对象),View Models(视图模型),或者直接是一些从一个service或者Web AP ...
 - 【LG4070】[SDOI2016]生成魔咒
			
[LG4070][SDOI2016]生成魔咒 题面 洛谷 题解 如果我们不用在线输的话,那么答案就是对于所有状态\(i\) \[ \sum (i.len-i.fa.len) \] 现在我们需要在线询问 ...
 - SQLServer数据库还原:无法在已有的mdf文件上还原文件
			
如果提示无法在已有的mdf文件上还原文件,请修改如下位置
 - 纯 CSS 解决自定义 CheckBox 背景颜色问题
			
CodePen 需要使用色 #ec6337(当然可以是任意颜色),解决问题:记住密码定制 CheckBox,解释全在注释里 主要使用到 ::before 或 ::after 伪类处理,伪装成内部的那个 ...
 - 《深入浅出NodeJS》mindmap
			
接触NodeJS有快两年了,但因为和我的工作内容关系不大,所以一直没有系统的学习.最近终于有空能系统地了解学习一下这门技术,于是买了一本朴灵老师的<深入浅出NodeJS>仔细研读.这本书内 ...
 - pthon自动化之路-编写登录接口
			
# Author:Lixiang Zoulock = "F:/Users/admin/PycharmProjects/day1/account.txt"account = &quo ...
 - BugkuCTF web2
			
前言 写了这么久的web题,算是把它基础部分都刷完了一遍,以下的几天将持续更新BugkuCTF WEB部分的题解,为了不影响阅读,所以每道题的题解都以单独一篇文章的形式发表,感谢大家一直以来的支持和理 ...
 - Keyshot+AD渲染PCB效果图
			
Keyshot+AD渲染PCB效果图 1.前言 前些天,公司同事找到我说,公司的展会宣传册要更新的了,有几款新的产品需要更新添加上去,大部分的新产品都有实物demo,可以拍照修一下图弄上去.但不巧,其 ...
 - 使用tensorflow实现mnist手写识别(单层神经网络实现)
			
import tensorflow as tf import tensorflow.examples.tutorials.mnist.input_data as input_data import n ...
 - ctf入门常见类别
			
原视频在这里:实验吧-名师指导http://www.shiyanbar.com/course-video/watch-video/cid/419/vid/2000网络安全从业者尝试介绍 web应用渗透 ...