C++成员函数的表
Tables of C++ member functions
我需要一个将代码映射到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。
这可能吗?怎样
是的,可以使用指向成员的指针语法。
使用您提供的原型,地图将是。
std::map< std::string, bool( foo::*)() const>
它将用这种语法称为
this->*my_map["somestring"]();
这个奇怪的->*
运算符用于指向成员函数的指针,由于继承的原因,这些函数可能会有一些奇怪的考虑因素。(这不仅仅是一个原始地址,正如->
所期望的那样)
由于您只需要存储具有相同参数和返回类型的同一类的成员,因此可以使用指向成员函数的指针:
bool foo::call(char const * name) const {
static std::map<std::string, bool (foo::*)() const> table
{
{"one", &foo::one},
{"two", &foo::two}
};
auto entry = table.find(name);
if (entry != table.end()) {
return (this->*(entry->second))();
} else {
return false;
}
}
它使用了C++11的新初始化语法。如果您的编译器不支持它,那么还有各种其他选项。你可以用一个静态函数初始化地图:
typedef std::map<std::string, bool (foo::*)() const> table_type;
static table_type table = make_table();
static table_type make_table() {
table_type table;
table["one"] = &foo::one;
table["two"] = &foo::two;
return table;
}
或者你可以使用Boost。分配:
static std::map<std::string, bool (foo::*)() const> table =
boost::assign::map_list_of
("one", &foo::one)
("two", &foo::two);
或者,您可以使用一个数组,并使用std::find_if
(如果您的库还没有,则使用简单的for
循环)或std::binary_search
(如果您确保数组已排序)查找条目。
是。
struct foo_method
{
std::string name;
bool (foo::*pfun)() const;
};
foo_method methodTable[] =
{
{ “somestring”, &foo::one },
{ ”otherstring”, &foo::one }
};
void foo::call(const char* name) const
{
size_t size = sizeof(methodTable)/sizeof(*methodTable);
for(size_t i = 0 ; i < size ; ++i)
{
if ( methodTable[i].name == name )
{
bool (foo::*pfun)() const = methodTable[i].pfun;
(this->*pfun)(); //invoke
}
}
}
我会选择boost::function
和std::map
。具体来说,类似这样的东西:
typedef boost::function<bool()> MyFunc;
typedef std::map<std::string, MyFunc> MyFuncMap;
然后,给定一个MyFuncMap的实例,您可以只执行map["something"]()
。然后可以将其封装在一个重载operator()
的类中。您可以使用函数指针/引用,但我更喜欢使用boost::function
,因为它允许我将指针绑定到成员函数(使用boost::bind)或使用其他函数对象。您也可以像使用常规函数指针一样,在条件语句中测试boost::function
。
以下是相关文件:
- 增压功能
- Boost.Bind
祝你好运!
编辑:关于你关于const成员和boost::function
的问题,这里有一个例子:
#include <boost/function.hpp>
#include <boost/bind.hpp>
typedef boost::function<bool ()> FuncPtr;
struct Test
{
bool test() const
{
std::cout << "yay" << std::endl;
}
};
int main(int argc, char **argv)
{
Test t;
FuncPtr ptr = boost::bind(&Test::test, &t);
ptr();
}
我只想补充一点,如果没有一个类的实例来调用它,指向成员函数的指针是没有意义的。你描述的情况说明了这一点(我想你也知道这一点),但在其他情况下,在某种函子构造中,可能需要用指向函数指针所对应实例的指针或引用来封装函数指针。
- 如何使用指针传递给函数的数组中对象的函数成员
- c++构造函数成员初始化:传递参数
- 创建 std::函数,它返回具有函数成员值的变量.分段错误
- 如何在C++通过公共函数访问私有函数成员?
- 解释了构造函数成员初始化列表
- 调用std::函数成员时内存损坏
- 是否可以为模板类的模板函数成员设置别名?
- 捕获 lambda 函数C++成员变量
- 构造函数成员初始值设定项跨成员列出,安全吗?
- 获取与在模板参数中传递的函数成员类型相同的类
- 如何从公共函数成员访问地图私有成员
- C 构造函数成员分配优化
- 使用命名空间进行函数成员定义
- 函数成员作为 CUDA 内核的参数
- 模板基类函数成员的别名
- 函数成员中用于void和继承的enable_if
- 头文件中是否定义了一个很长的Class函数成员
- 类内/构造函数成员初始化
- 使用指向部分专用函数成员的指针自动填充向量
- 指向函数成员的指针