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 ...
随机推荐
- xcode uml 工具
https://github.com/PaulTaykalo/objc-dependency-visualizer ./generate-objc-dependencies-to-json.rb -d ...
- Linux之 if命令——简单的shell文件
如何写一个shell文件,写一个小脚本 1.新建一个脚本文件:vi demo.sh 2.追加执行权限: chmod u+x demo.sh 3.执行脚本:./demo.sh 4.什么是脚本?把一堆命令 ...
- uva1609 Foul Play
思维 创造条件使一轮比赛之后仍满足1号打败至少一半,并剩下至少一个t' 紫书上的思路很清晰阶段1,3保证黑色至少消灭1半 #include<cstdio> #include<vect ...
- QT_1
QT概述 1.1 QT 是一个跨平台的C++图形用户界面应用程序框架 1.2 发展史: 1991奇趣科技 1.3 QT 版本:商业版.开源版 1.4 优点: 1.4.1 跨平台 1.4.2 接口简单 ...
- 工作流activi链接地址
http://topmanopensource.iteye.com/blog/1313865
- 企业支付宝账号开发接口教程--JAVA-UTF-8(实际操作完善中...SpringMVC+JSP)
关于即时到账的开发.审核通过.简单测试如下. 希望看的可以收藏或者赞一下哦. 1:拥有自己的支付宝企业账号.去产品商店选择适合自己的方案.并签约合同. 2:选择合适的商家收款产品并去签约.填写相应的信 ...
- POJ-1724 深搜剪枝
这道题目如果数据很小的话.我们通过这个dfs就可以完成深搜: void dfs(int s) { if (s==N) { minLen=min(minLen,totalLen); return ; } ...
- 如何用纯 CSS 创作一个冒着热气的咖啡杯
效果预览 在线演示 按下右侧的"点击预览"按钮在当前页面预览,点击链接全屏预览. https://codepen.io/zhang-ou/pen/xjXxoz 可交互视频教程 此视 ...
- python基础知识02-序列类型的方法
列表的方法: 增:append() insert() extend()只能添加序列类型. .改li[0]= '123' li.insert(2,'123') 2个参数,位置,值 li.remove(' ...
- Binary mod and divide(模拟+大数)
描述 Most people know that the binary operations. Do you know the binary mod and divide? Now give the ...