直接上例子,参考Effective c++ 和 https://blog.csdn.net/chdhust/article/details/8006601

#include <iostream>
#include <iomanip>
#include <tr1/memory>
#include <tr1/functional>//std::tr1::function 可以对静态成员函数进行绑定
//std::tr1::bind 可以对非静态成员函数进行绑定
//指向虚成员函数,可以实现函数重载 typedef std::tr1::function<void (int)> HandlerEvent;//然后再定义一个成员变量class Sharp{public:HandlerEvent handlerEvent;
};//然后在其它函数内就可以通过设置handlerEvent的值来动态装载事件响应函数了,如:class Rectangle{private:std::string name;Sharp sharp;
public:void initial(void);void initial_1(void);void initial_2(void);const Sharp getSharp() const;static void onEvent(int param){  std::cout << "invode onEvent method,get parameter: " << param << std::endl;}void onEvent_1(int param){  std::cout << "invode onEvent method,get parameter: " << param << std::endl;}virtual void onEvent_2(int param){std::cout << "invode Rectangle's onEvent method,get parameter: " << param << std::endl;}};//类的实现方法void Rectangle::initial(){sharp.handlerEvent = std::tr1::function<void (int)>(&Rectangle::onEvent); //sharp.handlerEvent = HandlerEvent(&Rectangle::onEvent); std::cout << "invode initial function!" << std::endl;
}
void Rectangle::initial_1(){sharp.handlerEvent = std::tr1::bind(&Rectangle::onEvent_1, this, std::tr1::placeholders::_1);//因onEvent函数需要一个参数,所以用一占位符,这里相当于int param这个参数std::cout << "invode initial function!" << std::endl;
}
void Rectangle::initial_2(){sharp.handlerEvent = std::tr1::bind(&Rectangle::onEvent_2, this, std::tr1::placeholders::_1);std::cout << "invode initial function!" << std::endl;
}const Sharp Rectangle::getSharp() const{return sharp;
}class Square : public Rectangle{public:void onEvent_2(int param){std::cout << "invode Square's onEvent method,get parameter: " << param << std::endl;}
};//下面为测试函数:int main(int argc,char *argv[]){Rectangle rectangle;rectangle.initial();  rectangle.getSharp().handlerEvent(23);   Rectangle rectangle_1;rectangle_1.initial_1();  rectangle_1.getSharp().handlerEvent(24);Rectangle rectangle_2;rectangle_2.initial_2();rectangle_2.getSharp().handlerEvent(25);Square square;square.initial_2();square.getSharp().handlerEvent(26);      return 0;
}

结果呈现:
invode initial function!
invode onEvent method,get parameter: 23
invode initial function!
invode onEvent method,get parameter: 24
invode initial function!
invode Rectangle’s onEvent method,get parameter: 25
invode initial function!
invode Square’s onEvent method,get parameter: 26

实现函数指针的功能,tr1::bind,tr1::function,virtual函数重载相关推荐

  1. 【c++】23.【函数指针】定义? 为什么不直接调用函数而要使用函数指针?

    1. 函数指针及其定义和用法,C语言函数指针详解 原文链接:http://c.biancheng.net/view/228.html 什么是函数指针 如果在程序中定义了一个函数,那么在编译时系统就会为 ...

  2. 函数指针及其的运用(上)——何为函数指针

    =========================引子========================= 我们都知道,数组名就是指向数组第一个元素的常量指针(详见<数组拾遗>).同理,对于 ...

  3. 函数指针--Nginx和Redis中两种回调函数写法

    1.Nginx和Redis中两种回调函数写法 #include <stdio.h>//仿Nginx风格 //结构外声明函数指针类型 typedef void (*ngx_connectio ...

  4. 函数指针amp;绑定: boost::functoin/std::function/bind

    see link: https://isocpp.org/wiki/faq/pointers-to-members function vs template: http://stackoverflow ...

  5. ios 调用c语言函数指针,第09天C语言(08):指向函数的指针的应用场景

    #pragma mark 指向函数的指针的应用场景 ###pragma mark 概念 ###pragma mark 代码 #include int sum(int v1,int v2) { retu ...

  6. std::tr1::function and std::tr1::bind

    std::tr1::function and std::tr1::bind 每日一话 一本正经的胡说八道 引言 在C++的TR1中(Technology Report)中包含一个function模板类 ...

  7. boost源码剖析之:泛型函数指针类boost::function(rev#3)

    boost源码剖析之:泛型函数指针类boost::function(rev#3) 刘未鹏 C++的罗浮宫(http://blog.csdn.net/pongba)   Note: 并非新作,03年曾放 ...

  8. 成员函数指针与高性能的C++委托 (Member Function Pointers and the Fastest Possible C++ Delegates)...

    标准C++中没有真正的面向对象的函数指针.这一点对C++来说是不幸的,因为面向对象的指针(也叫做"闭包(closure)"或"委托(delegate)")在一些 ...

  9. c语言函数指针封装函数,C语言之函数指针、回调函数的使用

    一.背景 首先看下如下代码,这个定义是放在头文件的,在程序中tCdrvCallbackFkt也定义了另一个变量,而且括号后面还跟定义了几个变量,不理解这个定义. typedef void (PUBLI ...

最新文章

  1. 算法----七进制数
  2. C#教程5:操作算子(1)
  3. jQuery-DOM操作之children()、next()、prev()、siblings()
  4. 八、IO优化(3)稀疏列
  5. hdu 1394 Minimum Inversion Number
  6. linux下使用hash_map及STL总结
  7. Android趣味课程:九宫格拼图游戏
  8. 文字栅格化剪切蒙版——创建文字遮罩效果的两种方法——梦幻游乐园
  9. 彻底删除手机系统软件
  10. 用the_excerpt处理中文文章字数限制的方法
  11. linux重要的文件操作
  12. 我的世界python写游戏_用python写游戏之 Give it up
  13. html5圆圈,圆形按钮HTML5/CSS3 button代码
  14. 【TUM公开数据集RGBD-Benchmark工具evaluate_rpe.py参数用法原理解读】
  15. [APIO2015] T1巴厘岛的雕塑
  16. eBPF用于Linux防火墙数据包过滤
  17. QQ在线咨询显示 未启用【解决方法】
  18. mysql数据库建站教程视频,Mysql数据库基础入门(附视频教程)
  19. 学Python必备的八个网址,快点码住收藏起来吧
  20. 一点一点学ASP.NET之基础概念——委托

热门文章

  1. 音频转换成mp3格式的步骤
  2. ztree通过ajax获取json并勾选checkbook
  3. mysql config.xml_generatorConfig-mysql.xml中连接数据库的正确书写方式。
  4. 第1章 Kotlin是什么
  5. vba 输出文本 m Linux,利用VBA实现EXCEL数据输出TXT等文本文件
  6. 曾经的习武少年,如今的锦佰安CEO:他立志要开启身份认证的无密时代
  7. 如何用C#+WinRAR 实现压缩
  8. 中规中矩的输入两个正整数m和n,求其最大公约数和最小公倍数。
  9. 使用nvm管理node和npm
  10. ApplicatioinRunner vs CommandLineRunner