最简单的“投射”方法是什么?指向 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是 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
andstd::mem_fun_ref
. They're templates that create functors that invoke the specified member function. TR1 adds anstd::tr1::bind
that's also useful and more versatile (and if you don't have TR1 available, that's based onBoost::bind
). C++0x will includestd::bind
in the standard library (virtually unchanged from TR1).听起来您想要像
boost::bind
这样的东西,将成员函数指针绑定到该类的实例。您愿意详细说明一下您想要做什么的问题吗?示例代码等?
It sounds like you want something like
boost::bind
, to bind the member function pointer to a instance of that class.Would you care to elaborate your question a bit as to what you're trying to do? Example code, etc.?
并使用 mem_fn()
and use mem_fn()