1. // testBind.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <boost/bind.hpp>
  5. #include <boost/function.hpp>
  6. #include <assert.h>
  7. #include <iostream>
  8. /*
  9. Title:boost::bind应用示例
  10. 示例运行环境:
  11. [1]boost 1.39 SDK
  12. [2]VisualStudio2008 + SP1
  13. 示例内容:
  14. Precondition:MySubClass实例方法 参数方法列表 已知
  15. Do:控制权从MyParentClass实例方法,转移到,MySubClass实例方法。
  16. */
  17. //MyDataType:自定义数据类型
  18. struct MyDataType{
  19. MyDataType() {}
  20. MyDataType(int nV):m_nV(nV) {}
  21. int m_nV;
  22. };
  23. //MyParentClass:框架Class,负责消息派发
  24. class MyParentClass{
  25. public:
  26. boost::function<int(int,MyDataType)> m_fSub;
  27. int CallSubClassFunc(int nDT,MyDataType mdtDT)
  28. {
  29. int nR = m_fSub(nDT,mdtDT);
  30. return nR;
  31. }
  32. };
  33. //MySubClass:业务逻辑层,负责消息处理
  34. class MySubClass{
  35. public:
  36. int Run(int nDT,MyDataType mdtDT)
  37. {
  38. return nDT+mdtDT.m_nV;
  39. }
  40. };
  41. int _tmain(int argc, _TCHAR* argv[])
  42. {
  43. MyParentClass mpc;
  44. MySubClass msc;
  45. //两种实例方法间,建立联系
  46. mpc.m_fSub=boost::bind<int>(&MySubClass::Run,msc,_1,_2);
  47. //触发调用
  48. int nR=mpc.CallSubClassFunc(2,MyDataType(3));
  49. assert(nR==5);
  50. return 0;
  51. }

http://blog.csdn.net/lee353086/article/details/5269910

boost::bind应用示例的更多相关文章

  1. boost::bind和boost::function使用示例

    C++11已支持bind和function,之前的不支持,但可以借助boost达到同样目的.看如下两段代码: 1) 创建HDFS目录 void hdfs::init() { if (0 == hdfs ...

  2. 使用BOOST BIND库提高C++程序性能

    Boost.Bind为函数和函数对象,值语义和指针提供语义了一致的语法.我们首先通过一些简单的例子来看看它的基本用法,之后我们会延伸到嵌套绑定以实现功能组合.理解bind用法的一个关键是理解占位符(p ...

  3. Boost::Thread使用示例 - CG-Animation - 博客频道 - CSDN.NET

    Boost::Thread使用示例 - CG-Animation - 博客频道 - CSDN.NET Boost::Thread使用示例 分类: C/C++ 2011-07-06 14:48 5926 ...

  4. boost asio 学习(二)了解boost::bind

    2.了解boost::bind使用boost::bind封装一个函数,考虑以下例子示例2a #include <iostream> #include <boost/bind.hpp& ...

  5. 【转】bind简单示例

    bind简单示例代码 namespace { class placeholder_ {}; placeholder_ __1; } template <typename R, typename ...

  6. boost::bind 详解

    使用 boost::bind是标准库函数std::bind1st和std::bind2nd的一种泛化形式.其可以支持函数对象.函数.函数指针.成员函数指针,并且绑定任意参数到某个指定值上或者将输入参数 ...

  7. 1,Boost -> Bind

    #include <boost/bind.hpp> #include <boost/shared_ptr.hpp> #include <iostream> usin ...

  8. boost::bind

    bind并不是一个单独的类或函数,而是非常庞大的家族,依据绑定的参数个数和要绑定的调用对象类型,总共有十个不同的形式,但它们的名字都叫bind. bind接受的第一个参数必须是一个可调用对象f,包括函 ...

  9. boost::bind 和 boost::function 基本用法

    这是一篇介绍bind和function用法的文章,起因是近来读陈硕的文章,提到用bind和function替代继承,于是就熟悉了下bind和function的用法,都是一些网上都有的知识,记录一下,期 ...

随机推荐

  1. CSS垂直居中的实现

    这个问题可以说是老生常谈了,面试时经常问道,一直没整理过,这次做个系统梳理 1.利用display:table实现 从caniuse.com上查到,display:table可以兼容到IE8,以目前环 ...

  2. ViewChild与ContentChild的联系和区别

    原文 https://www.jianshu.com/p/5ab619e576ea 大纲 1.认识ViewChild 2.认识ContentChild 3.ViewChild与ContentChild ...

  3. angular表单经验分享

    原文 https://www.jianshu.com/p/af359bd04f32 大纲 1.表单的名字不可以和传入的值的名字相同 2.在模板驱动表单的时候要使用angular表单的验证功能,需要将n ...

  4. [Now] Deploy a Node project with Zeit’s Now

    Use Zeit’s now to deploy a node application from your local machine to a remote cloud service in mom ...

  5. 前端js实现打印excel表格

    产品原型: 图片.png 功能需求:点击导出考勤表格按钮,会自动下载成Excel格式 图片.png 图片.png jsp页面代码: <div class="tools"> ...

  6. vimrum

    # Insert your preferred key mappings here.unmap <a-O>map <a-O> closeOtherTabsmap , previ ...

  7. [SCSS] Organize Styles with SCSS Nesting and the Parent Selector

    SCSS nesting can produce DRYer code by targeting child elements without having to write the parent c ...

  8. 一大波Java来袭(四)String类、StringBuilder类、StringBuffer类对照

    本文主要介绍String类.StringBuffer类.StringBuilder类的差别  : 一.概述 (一)String 字符串常量.可是它具有不可变性,就是一旦创建,对它进行的不论什么改动操作 ...

  9. js 复制文本的四种方式

    js 复制文本的四种方式 一.总结 一句话总结:js文本复制主流方法:document的execCommand方法 二.js 复制文本的四种方式 纯 转载复制,非原创 原地址:http://www.c ...

  10. javascript中window对象 部分操作

    <!--引用javascript外部脚本--> <script src="ss.js"></script> <script> //警 ...