std::tr1::function and std::tr1::bind

std::tr1::function and std::tr1::bind

每日一话

一本正经的胡说八道

引言

在C++的TR1中(Technology Report)中包含一个function模板类和bind模板函数,使用它们可以实现类似函数指针的功能,但却却比函数指针更加灵活,特别是函数指向类的非静态成员函数时。可以参考Scott Meyers. <<Effective C++ (3rd Edition)>>. Item 35.下面具体说明其使用方法。

指向全局函数或静态成员函数时

  1. 在本质上来讲全局函数和静态成员函数没有区别;
  2. 在使用方法上除了静态成员函数在引用时要在前面加域作用符className::外,没有其它任何区别,事实上全局函数也有可能放入命名空间,或者使用 全局域作用符,例如 nameSpace::function() 或::function,这样不仅本质上相同,形势上也与静态成员函数一致了。
  3. 所以它们是没有区别的,放到一起讨论。

这种情况比较简单,只需要定义一个类型

#include <iostream>
#include <iomanip>
#include <tr1/memory>
#include <tr1/functional>

typedef std::tr1::function<void (int)> HandlerEvent;
class Sharp{
public:
    HandlerEvent handlerEvent;
};

// 然后在其它函数内就可以通过设置handlerEvent的值来动态装载事件响应函数了,如:
class Rectangle{
private:
    std::string name;
    Sharp sharp;

public:
    void initial(void);
    const Sharp getSharp() const;
    static void onEvent(int param){  							 //---------------(1)
        std::cout << "invode onEvent method,get parameter: " << param << std::endl;
    }
};

// 类的实现方法
void Rectangle::initial(){
    sharp.handlerEvent = HandlerEvent(&Rectangle::onEvent); 		//---------------(2)
    std::cout << "invode initial function!" << std::endl;

}

const Sharp Rectangle::getSharp() const{
    return sharp;
}

// 下面为测试函数:
int main(int argc,char *argv[]){
    std::cout <<"hi: " << std::setw(50) << "hello world!" << std::endl;
    Rectangle rectangle;
    rectangle.initial();  										//---------------(3)
    rectangle.getSharp().handlerEvent(23);    					  //---------------(4)

}

//输出结果如下:
hi:                                       hello world!
invode initial function!
invode onEvent method,get parameter: 23    //---------------(5)

注 意,这里使用了静态成员函数,如果把Rectangle前面的static去掉这段代码不能工作,编译都不能通过,因为静态成员函数与非静态成员函数的参数表不一样,原型相同的非静态函数比静态成员函数多一个参数,即第一个参数this指针,指向所属的对象,任何非静态成员函数的第一个参数都是this指 针,所以如果把Rectangle前面的static去掉,其函数原型等效于下面的一个全局函数:

void onEvent(Rectangle* this, int);

所 以,这与HandlerEvent所声明的函数类型不匹配,编译将不能通过。而且,既然静态成员函数没有this指针,所以上面(3)处的调用使 sharp对象中的handlerEvent使向了Rectangle的静态方法onEvent(),这样当通过(4)处这样调用时就会自动执行(1)处 的静态函数onEvent()。

std::tr1::bind()模板函数的使用

通过上面的std::tr1::function 可以对静态成员函数进行绑定,但如果要对非静态成员函数的绑定,需用到下面将要介绍的bind()模板函数.

首先说bind的用法,其声明如下所示:

bind(Function fn, T1 t1, T2 t2, …, TN tN);

其中fn为将被调用的函数,t1…tN为函数的参数。如果不指明参数,则可以使用占位符表示形参,点位符格式为

std::tr1::placehoders::1,  std::tr1::placehoders::2,  …,  std::tr1::placehoders::_N	

将上例中Rectangle::onEvent(int param)前的static去掉改为非静态成员函数,则进行动态绑定使得程序正常运行,将Rectangle::initial(void)的定义修改为:

void Rectangle::initial(){
	sharp.handlerEvent = std::tr1::bind(&Rectangle::onEvent,this,std::tr1::placeholders::_1) //因onEvent函数需要一个参数,所以用一占位符;
    std::cout << "invode initial function!" << std::endl;
}

这样,便动态装载函数成功。其它测试数据都不用进行修改。测试结果于上一样。

三、指向虚成员函数的使用

对于虚成员函数的情况与上面第2节所说相同,仍然可以实现虑函数的效果。如果定义类Square继承自Rectangle,将 Rectangle::OnEvent重载,定义一个新的Square::OnEvent,Rectangle::initialize中的函数不变,仍然使用Rectangle::OnEvent进进绑定,则调用成员object.onEvent()时,具体执行Rectangle::OnEvent还 是Square::OnEvent,看object所属对象的静态类型是Rectangle还是Square而定.

下面为简单示例:

我们首先修改一个上面Rectangle的initial()方法,改为虚函数。如:

 virtual void onEvent(int param){

        std::cout << "invode Rectangle's onEvent method,get parameter: " << param << std::endl;

    }

然后我们再写一个Square类来继承Rectangle类。并重写onEvent方法。如:

class Square : public Rectangle{

public:

void onEvent(int param){

        std::cout << "invode Square's onEvent method,get parameter: " << param << std::endl;

    }

};

测试代码:

int main(int argc,char *argv[]){

    Rectangle rectangle;

    rectangle.initial();

    rectangle.getSharp().handlerEvent(23);

    Square square;

    square.initial();

    square.getSharp().handlerEvent(33);

}

运行后的结果如下:

hi:                                       hello world!

invode initial function!

invode Rectangle's onEvent method,get parameter: 23

invode initial function!

invode Square's onEvent method,get parameter: 33

这样我们就可以看到sharp会针对具体对象来调用相应的onEvent()方法。 上面的程序示例读者可自行研习。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值