C++ 表成员函数
我需要一个将代码映射到 C++ 成员函数的表。假设我们有这个类:
class foo
{
bool one() const;
bool two() const;
bool call(char*) const;
};
我想要的是一个像这样的表:
{
{ “somestring”, one },
{ ”otherstring”, two }
};
所以如果我有一个 foo
对象 f
, f.call(”somestring”)
将在表中查找“somestring”,调用 one()
成员函数,并返回结果。
所有被调用的函数都具有相同的原型,即它们是 const、不带参数并返回 bool。
这可能吗?如何?
I need a table that maps codes to C++ member functions. Suppose we have this class:
class foo
{
bool one() const;
bool two() const;
bool call(char*) const;
};
What I want is a table like this:
{
{ “somestring”, one },
{ ”otherstring”, two }
};
So that if I have a foo
object f
, f.call(”somestring”)
would look up “somestring” in the table, call the one()
member function, and return the result.
All of the called functions have identical prototypes, i.e., they are const, take no parameters, and return bool.
Is this possible? How?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,这是可能的,使用指向成员的指针语法。
使用您提供的原型,地图将是。
它将用这种语法来调用
看起来很奇怪的
->*
运算符用于指向成员函数的指针,由于继承的原因,它可能有一些奇怪的考虑因素。 (它不仅仅是一个原始地址,正如->
所期望的那样)Yes, it's possible, using pointer to member syntax.
Using the prototypes you supplied, the map would be.
It would be called with this syntax
That odd-looking
->*
operator is for pointer to member functions, which can have some odd considerations, due to inheritance. (It's not just a raw address, as->
would expect)由于您只需要存储具有相同参数和返回类型的同一类的成员,因此您可以使用指向成员函数的指针:
它使用 C++11 的新初始化语法。如果您的编译器不支持它,还有各种其他选项。您可以使用静态函数初始化地图:
或者您可以使用 Boost.Assignment:
或者您可以使用数组,并使用
std::find_if
查找条目(如果您的库没有,则使用简单的for
循环)还没有),或std::binary_search
如果您确保数组已排序。Since you only need to store members of the same class, with the same arguments and return types, you can use pointer-to-member-functions:
That uses the new initialisation syntax of C++11. If your compiler doesn't support it, there are various other options. You could initialise the map with a static function:
or you could use Boost.Assignment:
or you could use an array, and find the entry with
std::find_if
(or a simplefor
loop if your library doesn't have that yet), orstd::binary_search
if you make sure the array is sorted.是的。
Yes.
我会使用
boost::function
和std::map
。具体来说,是这样的:然后,给定一个 MyFuncMap 实例,您可以执行
map["something"]()
操作。然后您可以将其包装在一个重载operator()
的类中。您可以使用函数指针/引用,但我更喜欢使用 boost::function ,因为它允许我将指针绑定到成员函数(使用 boost::bind )或使用其他函数对象。您还可以像使用常规函数指针一样在条件中测试boost::function
。这是相关文档:
祝你好运!
编辑:关于您关于 const 成员和 boost::function 的问题,这里有一个示例:
I would go with
boost::function
withstd::map
. Concretely, something like this :Then, given an instance of MyFuncMap, you could just do
map["something"]()
. Then you could wrap that in a class that overloadsoperator()
. You could use function pointers/references, but I prefer usingboost::function
because it allows me to bind pointers to member functions (using boost::bind) or use other function objects. You can also testboost::function
in conditionals as you would with regular function pointers.Here is the relevant documentation :
Good luck!
Edit: Regarding your question about the const member and
boost::function
, here's an example :我想补充一点,如果没有类的实例来调用它,则指向成员函数的指针是没有意义的。您所描述的情况说明了这一点(我想您知道这一点),但是在其他情况下,可能需要用指针或对它对应于某种 函子构造。
I'd just like to add that a pointer to a member function is meaningless without having an instance of a class on which to call it. The situation you've described accounts for this (and I think you know this), however in other situations, it may be necessary to encapsulate the function pointer with a pointer or reference to the instance to which it corresponds in some sort of functor construct.