The example of callback in C++11 is shown below.

#include <functional>
void MyFunc1(int val1, int val2)
{
std::cout << __PRETTY_FUNCTION__ << val1 + val2 << std::endl;
} void MyFunc2(int val1, int val2)
{
std::cout << __PRETTY_FUNCTION__ << val1 + val2 << std::endl;
} void FunctionBindTest(void)
{
std::function<void(int)> pb = std::bind(MyFunc1, , std::placeholders::_1);
pb();
pb = std::bind(MyFunc2, , std::placeholders::_1);
pb();
}

The output:

void MyFunc1(int, int)11

void MyFunc2(int, int)12

Another example to show the message handler or can be used for ISR in embedded system design.

class CallbackC
{
private:
typedef std::function<void(std::string)> funcType;
public:
/**
* @brief Constructor
*/
CallbackC() = default; /**
* @brief Destructor
*/
~CallbackC() = default; /**
* @brief Set copy constructor as delete to prevent unintentional creation
*/
CallbackC(const CallbackC& iValue) = delete; /**
* @brief Set copy assignment as delete to prevent unintentional creation
*/
const CallbackC& operator=(const CallbackC& iValue) = delete;
static void RegisterMessage(uint8_t iMsgType, funcType iFunc);
static void ProcessMessage(uint8_t iMsgType, std::string iMsg);
private:
static funcType mCallbacks[];
};

in C++ source file.

CallbackC::funcType CallbackC::mCallbacks[];

void CallbackC::RegisterMessage(uint8_t iMsgType, funcType iFunc)
{
mCallbacks[iMsgType] = ifunc;
} void CallbackC::ProcessMessage(uint8_t iMsgType, std::string iMsg)
{
mCallbacks[iMsgType](iMsg);
} class SMSMessageC
{
public:
void Run(std::string iValue){std::cout << __PRETTY_FUNCTION__ << iValue <<std::endl;};
}; class MMSMessageC
{
public:
void Run(std::string iValue){std::cout << __PRETTY_FUNCTION__ << iValue << std::endl;};
}; void CallbackTest(void)
{
SMSMessageC sms;
MMSMessageC mms;
HW::CallbackC::RegisterMessage(, std::bind(&SMSMessageC::Run, &sms, std::placeholders::_1));
HW::CallbackC::RegisterMessage(, std::bind(&MMSMessageC::Run, &mms, std::placeholders::_1));
HW::CallbackC::ProcessMessage(, "my message");
HW::CallbackC::ProcessMessage(, "my message");
}

The output is:

void SMSMessageC::Run(std::__cxx11::string)my message

void MMSMessageC::Run(std::__cxx11::string)my message

So good! Is it? But the performance is lower than non-member or virtual member function call. The good idea is the SMSMessageC and MMSMessageC are not inherited from a same parent class.

Using C++11 function & bind的更多相关文章

  1. c++11 function bind 测试。

    实验小结 1)function 是一个模板类.有函数指针成员.可以看作安全型函数指针. template<typename _Res, typename... _ArgTypes> cla ...

  2. C++ 11: function & bind 使用示例

    #include <functional> #include <iostream> struct Foo { Foo(int num) : num_(num) {} void ...

  3. C++ 类的成员函数指针 ( function/bind )

    这个概念主要用在C++中去实现"委托"的特性. 但现在C++11 中有了 更好用的function/bind 功能.但对于类的成员函数指针的概念我们还是应该掌握的. 类函数指针 就 ...

  4. 【转帖】漫话C++0x(四) —- function, bind和lambda

    实在是觉得此文总是去翻感觉不太好.于是果断转过来了,想看原文的请戳:http://www.wuzesheng.com/?p=2032 本文是C++0x系列的第四篇,主要是内容是C++0x中新增的lam ...

  5. ES6下的Function.bind方法

    在JavaScript的使用中,this的指向问题始终是一个难点.不同的调用方式,会使this指向不同的对象.而使用call,apply,bind等方式,可改变this的指向,完成一些令人惊叹的黑魔法 ...

  6. Extjs使用Ext.function.bind, 给句柄函数传参

    回调函数updateImage中的key参数,在外部调用时有程序员自己指定. 使用Ext.Function.bind(this.updateImage, this, 'imageUrl', true) ...

  7. javascript 中 function bind()

    Function bind() and currying <%-- All JavaScript functions have a method called bind that binds t ...

  8. 为什么React事件处理函数必须使用Function.bind()绑定this?

    最近在React官网学习Handling Events这一章时,有一处不是很明白.代码如下: class Toggle extends React.Component { constructor(pr ...

  9. 学习C++11的一些思考和心得(1):lambda,function,bind和委托

     1.lambda表达式 lanbda表达式简单地来讲就是一个匿名函数,就是没有名称的函数,如果以前有接触过python或者erlang的人都比较熟悉这个,这个可以很方便地和STL里面的算法配合 st ...

随机推荐

  1. String类的常用方法总结

    一.String类String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象.java把String类声明的final类,不能有类.String类对象创建 ...

  2. 使用keytool生成公钥、私钥、证书并且读取出来,使用私钥签名jar并验证(转)

    参考链接:http://happyqing.iteye.com/blog/2139504 :https://blog.csdn.net/arjelarxfc/article/details/52461 ...

  3. angular2组件通讯的几种方式

    最近刚刚接触angular2,对ng2也是一知半解,如有说得不对的地方欢迎指出,欢迎加q共同探讨学习991085978: 1.通过输入型绑定把数据从父组件传到子组件 HeroChildComponen ...

  4. day 36 关于io模型的问题 阻塞 和多路复用

    # from gevent import spawn,monkey;monkey.patch_all()# from socket import *# def server(ip,port):# se ...

  5. 搭建VUE项目

    1.换源由于npm源服务器在国内访问速度较慢,所以一般需要更换源服务器地址npm config set registry https://registry.npm.taobao.org也可以安装cnp ...

  6. 栈溢出原理与 shellcode 开发

     ESP:该指针永远指向系统栈最上面一个栈帧的栈顶  EBP:该指针永远指向系统栈最上面一个栈帧的底部 01  修改函数返回地址 #include<stdio.h> #include< ...

  7. CentOS7+Nginx配置Tomcat负载均衡环境

    1.准备两个Tomcat 配置两个Tomcat一个端口是8080另外一个端口是8081,分别在webapps下面添加一个测试用的web项目,修改index.jsp文件,8080端口的index.jsp ...

  8. HDU 6050 17多校2 Funny Function(数学+乘法逆元)

    Problem Description Function Fx,ysatisfies:For given integers N and M,calculate Fm,1 modulo 1e9+7.   ...

  9. sequence to sequence模型

    sequence to sequence模型是一类End-to-End的算法框架,也就是从序列到序列的转换模型框架,应用在机器翻译,自动应答等场景. Seq2Seq一般是通过Encoder-Decod ...

  10. Ubuntu16.04阿里云源

    16.04源 deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse deb http://m ...