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. x多进程

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- ''' from multiprocessing import Process import os #子进 ...

  2. [Linux]Linux下开启snmp支持IPV4和IPV6

    SNMP简介 简单网络管理协议(SNMP),由一组网络管理的标准组成,包含一个应用层协议(application layer protocol).数据库模型(database schema)和一组资源 ...

  3. Excel 数据读入到DataSet

    using System; using System.Collections.Generic; using System.Linq; using System.Data; using System.I ...

  4. sass的加减乘除

    运算 时单位要一样 否则报错 除法时 要加() width: (100px / 2); 符号在已有的数学表达式中时,也会被认作除法符号,就不要加() .box { width: 100px / 2 + ...

  5. python学习二三事儿(转,整)

    Python 标识符 在 Python 里,标识符由字母.数字.下划线组成. 在 Python 中,所有标识符可以包括英文.数字以及下划线(_),但不能以数字开头. Python 中的标识符是区分大小 ...

  6. java8 字符串转换 list long Integer

    String ids= "1,2,3,4,5,6"; List<Long> listIds = Arrays.asList(ids.split("," ...

  7. 编译varnish 报No package 'libpcre' found

    pcre的lib目录未指定 假如安装pcre的目录为/usr/local/pcre 那么lib目录为/usr/local/pcre/lib export PKG_CONFIG_PATH=/usr/lo ...

  8. UBUNTU安装 Rabbitvsc可视化版本控制客户端软件

    sudo add-apt-repository ppa:rabbitvcs/ppa sudo apt-get update sudo apt-get install rabbitvcs-core ra ...

  9. Capjoint的merrcmd生成二次曲线的misfit原理

    http://www.personal.psu.edu/jhm/f90/lectures/lsq2.html

  10. 『转』VC++ webbrowser函数使用范例

    /*============================说明部分================================= 实现一下函数需包含头文件 #include <Winine ...