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. (C/C++学习笔记) 六. 表达式

    六. 表达式 ● 表达式 表达式 expression An expression consists of a combination of operators and operands. (An o ...

  2. xilinx 高速收发器Serdes深入研究-Comma码(转)

    一.为什么要用Serdes 传统的源同步传输,时钟和数据分离.在速率比较低时(<1000M),没有问题. 在速率越来越高时,这样会有问题 由于传输线的时延不一致和抖动存在,接收端不能正确的采样数 ...

  3. 基于MicroBlaze 的嵌入式系统设计

       reference: http://xilinx.eetrend.com/d6-xilinx/article/2013-03/3863.html 摘 要:当今时代,嵌入式系统已经无所不在,与人们 ...

  4. 【python】pandas display选项

    import pandas as pd 1.pd.set_option('expand_frame_repr', False) True就是可以换行显示.设置成False的时候不允许换行 2.pd.s ...

  5. JavaWeb:脚本标识

    脚本标识 一.JSP表达式 1.介绍 用于向页面中输出信息 2.语法格式 <%= 表达式%> 3.注意 在"<%"和"="之间不允许有空格,但 ...

  6. 【Python】数据库练习-1

    三十四 数据库 1.     查看数据库命令 2.     使用某个数据库 3.     查看当前在哪个库 4.     查看当前数据库中有哪些表 5.     查询表中数据 6.     建库:cr ...

  7. 【Python】管道通信和condition

    #练习:管道练习,双工,单工,将受到的消息保存到文件中 import multiprocessing as mp from multiprocessing import Process,Lock de ...

  8. SQL注入之Sqli-labs系列第八篇(基于布尔盲注的注入)

    开始挑战第八关(Blind- Boolian- Single Quotes- String) 这关首先需要用到以下函数: 开始测试,使用测试语句,利用单引号进行闭合 猜解字段 union select ...

  9. 5个适用于初学者的有用数据分析表达式(DAX)函数

    数据分析表达式(DAX)入门可能令人生畏,但是,当你了解一些基本功能后,你就可以帮助你解答许多有关数据的新见解.虽然在Power BI或Pivot Charts中创建视觉效果很容易,但我们经常希望查看 ...

  10. Oracle数据库select语句

    select * from EMp--all data in EMP table select * from EMP where ename in('SMITH')--the data where e ...