函数指针&绑定: boost::functoin/std::function/bind
see link: https://isocpp.org/wiki/faq/pointers-to-members 
function vs template: http://stackoverflow.com/questions/14677997/stdfunction-vs-template
boost::functoin/std::function可用于全部 operator() 操作的对象(函数,类。成员函数。lambda表达式等等)。
用处就是能够使用一个函数指针调用不用的函数实体(仅仅要他们的signature一样),实现回调函数。或者多种不同的算法等等。 
关于 std::function的实现。 see link:http://stackoverflow.com/questions/18453145/how-is-stdfunction-implemented 
非常好的样例:原文链接
#include <functional>
#include <iostream>
using namespace std;
std::function< int(int)> Functional;
// 普通函数
int TestFunc(int a)
{
    return a;
}
// Lambda表达式
auto lambda = [](int a)->int{ return a; };
// 函数对象(functor)
class Functor
{
public:
    int operator()(int a)
    {
        return a;
    }
};
// 1.类成员函数
// 2.类静态函数
class TestClass
{
public:
    int ClassMember(int a) { return a; }
    static int StaticMember(int a) { return a; }
};
int main()
{
    // 普通函数
    Functional = TestFunc;
    int result = Functional(10);
    cout << "普通函数:"<< result << endl;
    // Lambda表达式
    Functional = lambda;
    result = Functional(20);
    cout << "Lambda表达式:"<< result << endl;
    // 仿函数
    Functor testFunctor;
    Functional = testFunctor;
    result = Functional(30);
    cout << "仿函数:"<< result << endl;
    // 类成员函数
    TestClass testObj;
    Functional = std::bind(&TestClass::ClassMember, testObj, std::placeholders::_1);
    result = Functional(40);
    cout << "类成员函数:"<< result << endl;
    // 类静态函数
    Functional = TestClass::StaticMember;
    result = Functional(50);
    cout << "类静态函数:"<< result << endl;
    return 0;
}function简化了函数指针的使用:
class FooClass {
public:
     void Print( int a ) {
         std::cout << "A FooClass, param = "<< a <<" this = " << this << std::endl;
     }
};
void main() {
    FooClass *myFoo = new FooClass();
    void( FooClass::* oldFunc )(int) = &FooClass::Print; //C style function pointer
    (myFoo->*oldFunc)( 5 );
    boost::function newFunc = boost::bind( &FooClass::Print, myFoo, _1 ); //boost function
    newFunc( 5 );
}函数指针&绑定: boost::functoin/std::function/bind的更多相关文章
- 从成员函数指针生成可调用对象:function<>、mem_fn()和bind()
		我们知道,普通函数指针是一个可调用对象,但是成员函数指针不是可调用对象.因此,如果我们想在一个保存string的vector中找到第一个空string,不能这样写: vector<string& ... 
- 第12课 std::bind和std::function(3)_std::function可调用对象包装器
		1. std::function (1)首先是一个类模板,用于包装可调用对象.可以容纳除了类成员(函数)指针之外的所有可调用对象. (2)可以将普通函数,lambda表达式和函数对象类统一起来.尽管它 ... 
- typedef 函数指针 数组 std::function
		1.整型指针 typedef int* PINT;或typedef int *PINT; 2.结构体 typedef struct { double data;}DATA, *PDATA; //D ... 
- c++11 符号修饰与函数签名、函数指针、匿名函数、仿函数、std::function与std::bind
		一.符号修饰与函数签名 1.符号修饰 编译器将c++源代码编译成目标文件时,用函数签名的信息对函数名进行改编,形成修饰名.GCC的C++符号修饰方法如下: 1)所有符号都以_z开头 2)名字空间的名字 ... 
- C++11 学习笔记 std::function和bind绑定器
		C++11 学习笔记 std::function和bind绑定器 一.std::function C++中的可调用对象虽然具有比较统一操作形式(除了类成员指针之外,都是后面加括号进行调用),但定义方法 ... 
- 成员函数指针与高效C++委托 (delegate)
		下载实例源代码 - 18.5 Kb 下载开发包库文件 - 18.6 Kb 概要 很遗憾, C++ 标准中没能提供面向对象的函数指针. 面向对象的函数指针也被称为闭包(closures) 或委托(del ... 
- 剖析std::function接口与实现
		目录 前言 一.std::function的原理与接口 1.1 std::function是函数包装器 1.2 C++注重运行时效率 1.3 用函数指针实现多态 1.4 std::function的接 ... 
- 【浅析C++11】std::function和std::bind
		目录 std::function可调用对象包装器 std::function基本用法 std::function/std::bind与抽象工厂.工厂方法的一点思考 std::function可调用对象 ... 
- C++11之std::function和std::bind
		std::function是可调用对象的包装器,它最重要的功能是实现延时调用: #include "stdafx.h" #include<iostream>// std ... 
随机推荐
- Mybatis 的Log4j日志输出问题 - 以及有关日志的所有问题
			使用Mybatis的时候,有些时候能输出(主要是指sql,参数,结果)日志.有些时候就不能. 无法输出日志的时候,无论怎么配置log4j,不管是properties的还是xml的,都不起作用. 有些时 ... 
- Sicily-1009 梅森素数
			一.梅森素数 素数有无穷多个,却只有极少量的素数能表示成2p-1(p为素数)的形式.在不大于257的素数中,当p=2.3.5.7.13.17.19.31.67.127.257时,2p-1是素数,其它都 ... 
- BZOJ 1622: [Usaco2008 Open]Word Power 名字的能量
			题目 1622: [Usaco2008 Open]Word Power 名字的能量 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 349 Solved ... 
- Air Raid(最小路径覆盖)
			Air Raid Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7511 Accepted: 4471 Descript ... 
- Hive实现oracle的Minus函数
			在Oracle中minus运算的主要功能是: 在进行两个表格或者两个查询结果的时候,返回在第一个表格/查询结果中与第二个表格/查询结果不同样的记录. 结果不同样的记录包括两种情况:A,B 表中某一行的 ... 
- poj1742 多重背包的可行性问题
			http://poj.org/problem? id=1742 Description People in Silverland use coins.They have coins of value ... 
- 倒计时IE10+
			直接上代码,dome 里边有我做的列表倒计时(多个同时倒计时)下面是我做的例子,颜色可以自己设置的 <p name="daojishi" style="width: ... 
- AzureDev 社区活动获奖者公布
			今天,我们高兴地宣布 AzureDev社区活动的获奖者,并向这 5 个非盈利技术教育组织发放 10 万美元奖金.在 2013 年的Build大会上宣布的 AzureDev 活动专注于通过代码改变世界, ... 
- ASP.NET页面之间传值
			介绍: 在网页应用程序的开发中,页面之间的传值应该是最常见的问题了. 在这篇文章里,azamsharp 将为我们介绍一些ASP.NET页面传值的方式.本文所举的例子非常简单,仅仅包含了一个文本框和几个 ... 
- Ext.net中常用的三种交互方式
			http://www.ext.net.cn/forum.php?mod=viewthread&tid=10433 
