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. Android简易项目--傻瓜式阿拉伯语输入法(Dummy Arabic Input)

    一.应用名称 Android简易项目--傻瓜式阿拉伯语输入法(Dummy Arabic Input) 二.应用图标 三.应用说明 现在通行的阿拉伯语键盘布局并无规律可循,阿拉伯语使用者需要花费较多时间 ...

  2. Android: apk反编译 及 AS代码混淆防反编译

    一.工具下载: 1.apktool(资源文件获取,如提取出图片文件和布局文件) 反编译apk:apktool d file.apk –o path 回编译apk:apktool b path –o f ...

  3. 解决无法创建 JPA 工程的问题

    原创播客,如需转载请注明出处.原文地址:http://www.cnblogs.com/crawl/p/7703803.html ------------------------------------ ...

  4. 强化学习9-Deep Q Learning

    之前讲到Sarsa和Q Learning都不太适合解决大规模问题,为什么呢? 因为传统的强化学习都有一张Q表,这张Q表记录了每个状态下,每个动作的q值,但是现实问题往往极其复杂,其状态非常多,甚至是连 ...

  5. MVC无刷新上传图片并显示

    @{ ViewBag.Title = "Home Page"; } <script src="../../Scripts/swfobject.js" ty ...

  6. Oracle hint手动优化

    例子 select/*+FULL(fortest)*/ * from fortest where id = 2000000 //使用0.70s时间 select* from fortest where ...

  7. 2019.3.22 Week 12 : ZigBee and T/H chamber test

    Test purposes Remove backside center ventilation holes, pls help to conduct climatic chamber test of ...

  8. TensorFlow随机值函数:tf.random_uniform

    tf.random_uniform 函数 random_uniform( shape, minval=0, maxval=None, dtype=tf.float32, seed=None, name ...

  9. python如何进行内存管理的

    python引用了一个内存池(memory pool)机制,即pymalloc机制(malloc:n,分配内存),用于管理对小块的申请和释放.

  10. KB/MB/GB。。单位换算

    今天遇到一个需求,需要把数据单位进行换算,记录一下.写的不好请勿见怪. function bytesToSize( bytes ) {//单位转化         var k = 1024,      ...