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. 3.3 C++改变基类成员在派生类中的访问属性

    参考:http://www.weixueyuan.net/view/6360.html 总结: 使用using声明可以改变基类成员在派生类中的访问属性. private: using book::se ...

  2. Android开发 ---Activity的7种运行状态

     Android开发 ---Activity的7种运行状态 创建 --> 启动 --> 运行 -->  暂停 --> 停止 --> 销毁 重启 操作图解: 1.MainA ...

  3. MySQL输入密码后闪退

    刚刚我遇到这个问题,服务里MySQL是启状态的,所以我求助百度,发现很多种说法,我试了几个,还是不行,后来想起来我的密码不对,于是换了正确的密码试了一下,没毛病,进去了. 所以输入密码闪退时,首先确定 ...

  4. 第四周四则运算3 PSP表格

    PSP2.1 Personal Software Process Stages time Plan 计划   -Estimate 整数四则运算 分数四则运算 括号 括号四则运算 在主函数中调用 30m ...

  5. BSTR 转 char*

    #include <comdef.h> #include <comutil.h> #pragma comment(lib,"comsuppw.lib") _ ...

  6. centos的mysql升级之后密码重置

    1.配置文件添加过滤密码选项 #vim  /etc/my.cnf 跳过密码校验 2.重启mysql服务 #/etc/init.d/mysqld  restart 3.#mysql -uroot -p ...

  7. crontab的定时任务实例

    实例1:每1分钟执行一次myCommand * * * * * myCommand 实例2:每小时的第3和第15分钟执行 3,15 * * * * myCommand 实例3:在上午8点到11点的第3 ...

  8. angular 学习日志

    1.创建项目 npm install -g @angular/cli ng new my-app cd my-app ng serve --open // 或者 npm start 2.生成新模块 n ...

  9. 16 多校 8 Ball (贪心排序)很巧妙的思路啊~

    ZZX has a sequence of boxes numbered 1,2,...,n1,2,...,n. Each box can contain at most one ball. You ...

  10. 对于src路径问题,深层理解的实践。且对于输出流write()两个方法的源码阅读。

    根据昨天的总结,可深层理解图片中src的路径.所以今天实现了一个想法.就是路径写入的是Controller,然后自动去本地找. 其实就是将电脑的本地图片 显示出来.通过输出流的方式. 代码如下: @R ...