简单例子:
class tt
{
public:
void foo(int x) { printf("%d",x); }
};
typedef void ( tt::* FUNCTYPE)(int );
FUNCTYPE ptr = tt::foo; //给一个成员函数指针赋值.
tt a;
(a.*ptr)(5);//调用成员函数指针.
tt *b = new tt;
(b->*ptr)(6); //调用成员函数指针.
men_fun与mem_fun_ref
#include<iostream>
using namespace std;
#include<functional>
#include<algorithm>
#include<bitset>
#include<vector>
class A
{
public:
int Fun()
{
cout<<"Fun"<<endl;
return 0;
}
};
void main()
{
vector<A*> vectA;
for(int i=1;i<3;i++)
{
A * a=new A;
vectA.push_back(a);
}
for_each(vectA.begin(),vectA.end(),mem_fun(&A::Fun));// mem_fun_ref的作用和用法跟mem_fun一样,唯一的不同就是:当容器中存放的是对象实体的时候用mem_fun_ref,当容器中存放的是对象的指针的时候用mem_fun。
}
我们把men_fun与mem_fun_ref理解为函数绑定器就够了!!
模板成员函数
class Obj
{
public:
template <class T>
void display(const T& val1 = 10, int val2 = 10) {
cout << val1 << endl;
cout << val2 << endl;
}
};
int main()
{
Obj obj;
string str( "hello ");
obj.display(str, 100);
system("pause");
return(0);
}
C++模板实现事件处理器中的“通用成员函数指针”的调用
参考:
http://www.cnblogs.com/Xiao_bird/archive/2009/07/03/1516441.html
class FuncCaller
{
public:
virtual void func_call(void* obj_ptr, void* param)=0;
};
template <typename T>
class FuncItem : public FuncCaller
{
private:
typedef void (T::*HandlerPtr)(void* param);
HandlerPtr handler;
public:
void func_call(void* obj_ptr, void* param)
{
if( !obj_ptr )
return ;
(((T*)obj_ptr)->*handler)(param);
}
FuncItem(const HandlerPtr ptr):handler(ptr) {} // 此处为新添加代码
};
typedef struct _EventRegister
{
EventType event;
FuncCaller* func_caller;
} EventRegister;
class ClassA
{
public:
void show_me(void* param)
{ printf("I'm ClassA, show_me is called! param is 0x%x\n", param); }
};
class ClassB
{
public:
void come_on(void* param)
{ printf("I'm ClassB, come_on is called! param is 0x%x", param); }
};
EventRegister event_center[] = {
{ EVENT_TEST_1 , new FuncItem<ClassA>(&ClassA::show_me) },
{ EVENT_TEST_2 , new FuncItem<ClassB>(&ClassB::come_on) },
{ EVENT_INVALID , NULL }
};
/**
* Function:send_event
* params:
* event -> event type
* target -> who will process the event
*/
void send_event(EventType event, void* target, void* param)
{
if( !target )
return ;
int loop_event = 0;
EventType et = event_center[loop_event].event;
while( et!=EVENT_INVALID )
{
if( et==event )
{
event_center[loop_event].func_caller->func_call(target, param);
break;
}
et = event_center[++loop_event].event;
}
}
void main()
{
ClassA ca;
ClassB cb;
send_event(EVENT_TEST_1, (void*)&ca, (void*)0xff00);
send_event(EVENT_TEST_2, (void*)&cb, NULL);
}
boost::bind
参考:
1,http://blog.csdn.net/Solstice/archive/2008/10/13/3066268.aspx
class Foo
{
public:
void methodA();
void methodInt(int a);
};
class Bar
{
public:
void methodB();
};
boost::function<void()> f1; // 无参数,无返回值
Foo foo;
f1 = boost::bind(&Foo::methodA, &foo);
f1(); // 调用 foo.methodA();
Bar bar;
f1 = boost::bind(&Bar::methodB, &bar);
f1(); // 调用 bar.methodB();
f1 = boost::bind(&Foo::methodInt, &foo, 42);
f1(); // 调用 foo.methodInt(42);
boost::function<void(int)> f2; // int 参数,无返回值
f2 = boost::bind(&Foo::methodInt, &foo, _1);
f2(53); // 调用 foo.methodInt(53);