Archive for October, 2007

diff 不同符号的含义

〈- 红色 表示 此行被移出
-〉 黄色 表示 此行是新移入的
〈! 红色 此行被删除,并且修改过
!〉 黄色 表示 此行是新增加的,并且修改过

评论

boost function 和 bind的用法的小例子

#include "stdafx.h"
#include
#include

//函数对象,重载()
struct int_div
{
float operator()(int x, int y) const { return ((float)x)/y; };
};

//自由函数
float free_div(int x, int y)
{
{ return ((float)x)/y; };
}

//成员函数
class CDiv
{
public:
float div(int x, int y)
{

return ((float)x)/y;
}

};

void test1(void)
{

//函数对象的普通用法
int_div div;
div(3,4);

//声明一个函数指针
boost::function< float(int,int )> f;

//函数对象的赋值
f=int_div();

f(1,2);
f(3,4);

//自由函数的赋值
f=free_div;
f(1,2);
f(3,4);

//f= &CDiv::div;
//编译无法通过,但如果能有此格式,最好不过

//只能用以下比较苯的方法了

//方法1 用bind
CDiv theDiv;

//类实例的地址绑定
f=boost::bind(&CDiv::div,&theDiv,_1,_2);

//类实例的赋值绑定
f=boost::bind(&CDiv::div,theDiv,_1,_2);

//方法2 重新声明一个function
//function 对成员函数的声明
boost::function< float(CDiv *,int,int )> pF;

//function 对成员函数的赋值
pF=&CDiv::div;

//function 对成员函数的调用
pF(&theDiv,1,2);

}

int _tmain(int argc, _TCHAR* argv[])
{
test1();
return 0;
}

评论


0.079 sec