C++ std::tr1::bind使用
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使用的更多相关文章
- std::tr1::function和bind组件
C++中std::tr1::function和bind 组件的使用 在C++的TR1中(Technology Report)中包含一个function模板类和bind模板函数,使用它们可以实现类似函数 ...
- std::tr1::function
转自:https://www.cnblogs.com/qlee/archive/2011/07/04/2097594.html 在C++的TR1中(Technology Report)中包含一个fun ...
- C++ std::tr1::shared_ptr使用说明
1. 介绍 shared_ptr 是通过指针保持某个对象的共享拥有权的智能指针. 若干个 shared_ptr 对象能够拥有同一个对象:最后一个指向该对象的 shared_ptr 被销毁或重置时.该对 ...
- c++智能指针《二》 std::tr1::shared_ptr
转载http://www.cnblogs.com/kadinzhu/archive/2011/12/12/2284826.html 看<effective c++>,作者一直强调用std: ...
- 函数指针&绑定: boost::functoin/std::function/bind
see link: https://isocpp.org/wiki/faq/pointers-to-members function vs template: http://stackoverflow ...
- gtest 1.7编译错误:std:tr1:tuple模板参数过多的解决方案
在gtest/gtest.h文件中添加如下代码 #define _VARIADIC_MAX 10
- 使用tr1的bind函数模板
最近把公司的VS2008统一升级为SP1了,虽然还是有些跟不上时代,毕竟C++17标准都出了,但是,对于成熟的商业软件开发而言,追求更新的C++标准肯定不是正道.升级SP1的VS2008可以支持TR1 ...
- C++中str1::function和bind
在C++的TR1中(TechnologyReport)中包括一个function模板类和bind模板函数,使用它们能够实现类似函数指针的功能,但却却比函数指针更加灵活,特别是函数指向类的非静态成员函数 ...
- [转] [翻译]图解boost::bind
http://kelvinh.github.io/blog/2013/12/03/boost-bind-illustrated/ 其实这是很久之前留的一个坑了,一直没有填.. 记得在刚开始看到 boo ...
随机推荐
- (转)Spring管理的Bean的生命周期
http://blog.csdn.net/yerenyuan_pku/article/details/52834011 bean的初始化时机 前面讲解了Spring容器管理的bean的作用域.接着我们 ...
- Boxes And Balls(三叉哈夫曼编码)
题目 原题链接:http://codeforces.com/problemset/problem/884/D 现有一堆小石子,要求按要求的数目分成N堆,分别为a1.a2....an.具体的,每次选一个 ...
- scroll offset & client总结
oEvent.clientX 是指鼠标到可视区左边框的距离. oEvent.clientY 是指鼠标到可视区上边框的距离. clientWidth 是指可视区的宽度. clientHeight 是 ...
- regular expression matching DP
这个题目,我从前天晚上(8月6号晚上)调试到现在(8月8号16:21),太心酸了,不好好总结一下,就太对不起自己了! 这是题目: Implement regular expression matchi ...
- Swift详解之NSPredicate
言:谓词在集合过滤以及CoreData中有着广泛的应用.本文以Playground上的Swift代码为例,讲解如何使用NSPredicate. 准备工作 先在Playground上建立一个数组,为后文 ...
- 深入Linux内核架构——进程管理和调度(上)
如果系统只有一个处理器,那么给定时刻只有一个程序可以运行.在多处理器系统中,真正并行运行的进程数目取决于物理CPU的数目.内核和处理器建立了多任务的错觉,是通过以很短的间隔在系统运行的应用程序之间不停 ...
- spring-mvc junit测试
import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; impor ...
- python基础知识03-格式化输出和深浅复制
VIM中HJKL可以上下左右移动光标. 格式化输出和深浅复制 1.字符串的拼接和格式化 sudo pip3 install ipython 安装 ipython 进入 字符串相加 str1 + str ...
- vim 系列
http://blog.csdn.net/laogaoav/article/details/17370269 非常不错
- python之Gui编程事件绑定 2014-4-8
place() 相对定位与绝对定位 相对定位 拖动会发生变化 绝对定位不会from Tkinter import *root = Tk()# Absolute positioningButton(ro ...