一句话:container<ClassType*>就用mem_fun,container<ClassType>就用mem_fun_ref
参考:
http://www.cplusplus.com/reference/functional/mem_fun/
http://www.cplusplus.com/reference/functional/mem_fun_ref/
其实这里的本质可以看这篇随笔:
http://www.cnblogs.com/qrlozte/p/4453786.html
假设我们把这篇随笔的dosomething_five改为:void dosomething_five(MemberFuncType ptr, MyClass* obj)
实现如下:
void dosomething_five(MemberFuncType ptr, MyClass *obj) { (obj->*ptr)(5); }
假设改为:void dosomething_five(MemberFuncType ptr, MyClass &obj)
实现如下:
void dosomething_five(MemberFuncType ptr, MyClass &obj) { MyClass *p_obj = &obj; (p_obj->*ptr)(5); }
这其实就是mem_fun和mem_fun_ref的区别
时间: 2024-10-29 19:09:48