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 ...
随机推荐
- 图解在Ubuntu16.04中安装MySQL
1.安装mysql-server sudo apt-get install mysql-server 输入y,回车.等待下载,安装 出现下图,需要设定Mysql的密码. 输入密码,回车,弹出确认对话框 ...
- C++静态全局变量和全局变量的区别
静态全局变量 非静态全局变量 存储方式 静态存储 静态存储 作用域 定义该变量的源文件内 所有源文件 解释: 共同点:全局变量(外部变量)的说明之前再冠以static 就构 成了静态的全局变量.全 ...
- myBatis参数处理 myBatis佟刚课程笔记
单个参数:myBatis不会做特殊处理 #{参数名}: 取出参数值 多个参数: myBatis会做特殊处理 多个参数会被封装成一个MAP key:param1 param2.... param10,或 ...
- 报错:org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n ...
- python获取指定文件夹下的文件路径
#!/usr/bin/python# -*- coding: UTF-8 -*-# @date: 2018/1/6 23:08# @name: tmp2# @author:vickey-wu impo ...
- spring中注解的实现原理
@Autowired和@Resource的区别: 在Java中使用@Autowired和@Resource注解进行装配,这两个注解分别是:1.@Autowired按照默认类型(类名称)装配依赖对象,默 ...
- CF550 DIV3
A - Diverse Strings CodeForces - 1144A A string is called diverse if it contains consecutive (adjace ...
- python TCP协议详解 三次握手四次挥手和11种状态
11种状态解析 LISTEN -------------------- 等待从任何远端TCP 和端口的连接请求. SYN_SENT --------------- 发送完一个连接请求后等待一个 ...
- ubuntu18.04 server配置静态ip
最新发布的ubuntu18.04 server,启用了新的网络工具netplan,对于命令行配置网络参数跟之前的版本有比较大的差别,现在介绍如下:1.其网络配置文件是放在/etc/netplan/50 ...
- jquery validate基本
http://www.runoob.com/jquery/jquery-plugin-validate.html jquery validate 默认 在键盘按下并释放及提交后验证提交表单 例如: $ ...