1. 简述

  同function函数相似。bind函数相同也能够实现相似于函数指针的功能。但却却比函数指针更加灵活。特别是函数指向类 的非静态成员函数时。std::tr1::function 能够对静态成员函数进行绑定,但假设要对非静态成员函数的绑定,需用到下机将要介绍的bind()模板函数。

  bind的声明例如以下:

  

template<class Fty, class T1, class T2, ..., class TN>
unspecified bind(Fty fn, T1 t1, T2 t2, ..., TN tN);

  当中Fty为调用函数所属的类。fn为将被调用的函数,t1…tN为函数的參数。假设不指明參数,则能够使用占位符表示形參,占位符格式为std::tr1::placehoders::_1, std::tr1::placehoders::_2, …


2. 代码演示样例

  先看一下演示样例代码:

 

#include <stdio.h>
#include <iostream>
#include <tr1/functional> typedef std::tr1::function<void()> Fun;
typedef std::tr1::function<void(int)> Fun2; int CalSum(int a, int b){
printf("%d + %d = %d\n",a,b,a+b); return a+b;
} class Animal{
public:
Animal(){}
~Animal(){} void Move(){}
}; class Bird: public Animal{
public:
Bird(){}
~Bird(){} void Move(){
std::cout<<"I am flying...\n";
}
}; class Fish: public Animal{
public:
Fish(){}
~Fish(){} void Move(){
std::cout<<"I am swimming...\n";
} void Say(int hour){
std::cout<<"I have swimmed "<<hour<<" hours.\n";
}
}; int main()
{
std::tr1::bind(&CalSum,3,4)(); std::cout<<"--------------divid line-----------\n"; Bird bird;
Fun fun = std::tr1::bind(&Bird::Move,&bird);
fun(); Fish fish;
fun = std::tr1::bind(&Fish::Move,&fish);
fun(); std::cout<<"-------------divid line-----------\n";
//bind style one.
fun = std::tr1::bind(&Fish::Say,&fish,3);
fun(); //bind style two.
Fun2 fun2 = std::tr1::bind(&Fish::Say,&fish, std::tr1::placeholders::_1);
fun2(3); return 0;
}

  

  对于全局函数的绑定。可直接使用函数的地址,加上參数接口。std::tr1::bind(&CalSum,3,4)();。然后能够结合function函数。实现不同类成员之间的函数绑定。绑定的方式主要有代码中的两种。

  执行结果例如以下:

  

3 + 4 = 7

————–divid line———–

I am flying…

I am swimming…

————-divid line———–

I have swimmed 3 hours.

I have swimmed 3 hours.

C++ std::tr1::bind使用的更多相关文章

  1. std::tr1::function和bind组件

    C++中std::tr1::function和bind 组件的使用 在C++的TR1中(Technology Report)中包含一个function模板类和bind模板函数,使用它们可以实现类似函数 ...

  2. std::tr1::function

    转自:https://www.cnblogs.com/qlee/archive/2011/07/04/2097594.html 在C++的TR1中(Technology Report)中包含一个fun ...

  3. C++ std::tr1::shared_ptr使用说明

    1. 介绍 shared_ptr 是通过指针保持某个对象的共享拥有权的智能指针. 若干个 shared_ptr 对象能够拥有同一个对象:最后一个指向该对象的 shared_ptr 被销毁或重置时.该对 ...

  4. c++智能指针《二》 std::tr1::shared_ptr

    转载http://www.cnblogs.com/kadinzhu/archive/2011/12/12/2284826.html 看<effective c++>,作者一直强调用std: ...

  5. 函数指针&amp;绑定: boost::functoin/std::function/bind

    see link: https://isocpp.org/wiki/faq/pointers-to-members function vs template: http://stackoverflow ...

  6. gtest 1.7编译错误:std:tr1:tuple模板参数过多的解决方案

    在gtest/gtest.h文件中添加如下代码 #define _VARIADIC_MAX 10

  7. 使用tr1的bind函数模板

    最近把公司的VS2008统一升级为SP1了,虽然还是有些跟不上时代,毕竟C++17标准都出了,但是,对于成熟的商业软件开发而言,追求更新的C++标准肯定不是正道.升级SP1的VS2008可以支持TR1 ...

  8. C++中str1::function和bind

    在C++的TR1中(TechnologyReport)中包括一个function模板类和bind模板函数,使用它们能够实现类似函数指针的功能,但却却比函数指针更加灵活,特别是函数指向类的非静态成员函数 ...

  9. [转] [翻译]图解boost::bind

    http://kelvinh.github.io/blog/2013/12/03/boost-bind-illustrated/ 其实这是很久之前留的一个坑了,一直没有填.. 记得在刚开始看到 boo ...

随机推荐

  1. axios中为所有请求带上Token头

    axios中为所有请求带上Token头 https://www.imooc.com/article/27751

  2. QT+信号有参数与无参数的实现+QT4和QT5在信号和槽使用上的区别

    在QT5中,信号有参数和无参数 #ifndef SUBWIDGET_H #define SUBWIDGET_H #include <QWidget> #include <QPushB ...

  3. Maven实战读书笔记(七):Maven常用功能

    7.1.资源排除 <resources> <!-- 启动过滤,包含的文件会被过滤掉 --> <resource> <directory>src/main ...

  4. <a>标签的href 与 onclick 使用

    链接的onclick 事件被先执行,其次是href属性下的动作(页面跳转,或 javascript 伪链接): 假设链接中同时存在href 与onclick,如果想让href 属性下的动作不执行,on ...

  5. ArrayList集合(JDK1.8)

    简述 List是继承于Collection接口,除了Collection通用的方法以外,扩展了部分只属于List的方法. 常用子类  ?ArrayList介绍 1.数据结构 其底层的数据结构是数组,数 ...

  6. python计算圆面积

    #coding=gbk #coding=utf-8 #-*- coding: UTF-8 -*- #调用math包处理相关的运算 import math #圆半径 r = 2 #计算圆面积π*r*r与 ...

  7. Python的串口

    要使用python中的串口,可以下载pywin32-224-cp36-cp36m-win_amd64.whl去安装或者pip install去安装. 调试下来,有一点很不爽,读取read()数据的ti ...

  8. LeetCode(18)4Sum

    题目 Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...

  9. Leetcode 274.H指数

    H指数 给定一位研究者论文被引用次数的数组(被引用次数是非负整数).编写一个方法,计算出研究者的 h 指数. h 指数的定义: "一位有 h 指数的学者,代表他(她)的 N 篇论文中至多有 ...

  10. [Istio]Kubernetes集群部署Istio 1.0

    大部分内容都是可以根据https://istio.io/docs/setup/kubernetes/quick-start/来处理的,这里主要谈部署时一些细节的问题 首先,当我们按照 istio 官方 ...