什么是“铸造”的最简单的方式,一个成员函数指针指向C ++中的函数指针? [英] What is the simplest way to "cast" a member function pointer to a function pointer in C++?
问题描述
我想为STL算法的comp参数提供一个成员函数,如lower_bound(...,Compare comp)。 comp()函数访问非静态成员字段,因此它本身必须是非静态成员,但非静态成员函数指针的类型不同于普通函数指针的类型。
I want to provide a member function for the "comp" parameter of an STL algorithm like lower_bound( ..., Compare comp ). The comp() function accesses a non-static member field so it must itself be a non-static member but the type of a non-static member function pointer is different from that of an ordinary function pointer.
这个问题最好的方法是什么?
What is the best way around this problem?
推荐答案
code> std :: mem_fun 和 std :: mem_fun_ref
。它们是创建调用指定成员函数的函子的模板。 TR1添加了一个 std :: tr1 :: bind
,这也是有用的和更通用的(如果你没有TR1可用,这是基于 Boost :: bind
)。 C ++ 0x将在标准库中包含 std :: bind
(与TR1几乎没有变化)。
This is the most common use of std::mem_fun
and std::mem_fun_ref
. They're templates that create functors that invoke the specified member function. TR1 adds an std::tr1::bind
that's also useful and more versatile (and if you don't have TR1 available, that's based on Boost::bind
). C++0x will include std::bind
in the standard library (virtually unchanged from TR1).
这篇关于什么是“铸造”的最简单的方式,一个成员函数指针指向C ++中的函数指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!