是否有人可以解释基地*(*)()我在:什么是基础*(*)()
typedef std::map<std::string, Base*(*)()> map_type;
而且一个如何从一个函数返回呢?
我假设它是一个函数指针,返回一个Base *,但是这是什么(*)。指针函数返回Base*
:
我在以下SO张贴Is there a way to instantiate objects from a string holding their class name?
由于
是否有人可以解释基地*(*)()我在:什么是基础*(*)()
typedef std::map<std::string, Base*(*)()> map_type;
而且一个如何从一个函数返回呢?
我假设它是一个函数指针,返回一个Base *,但是这是什么(*)。指针函数返回Base*
:
我在以下SO张贴Is there a way to instantiate objects from a string holding their class name?
由于
Base* (*)()
是一种类型的发现这个。 *
表示它是一个指针,并且()
用于覆盖优先级,以确保指针适用于函数本身而不是返回类型。
您可以通过返回适当类型的函数的名称从函数返回它。
E.g.
Base* f();
Base* (*g())()
{
return f;
}
它的签名Base*()
的函数的函数指针的类型:
Base * foo();
Base * (*fp)() = &foo;
或者你的情况:
map_type callbacks;
callbacks["Foo"] = &foo;
要调用:
Base * p = callbacks["Foo"](); // same as "p = foo();"
感谢您的帮助,很遗憾,由于缺乏积分,我无法对您的答案进行投票。 – 2012-08-05 15:28:39
[ cdecl](http://cdecl.org/)说'Base *(*)()'i这是一个语法错误,很有趣。 – fredoverflow 2012-08-05 14:12:38
@FredOverflow:我认为我们必须假设'Base'被定义为一个类型。 – 2012-08-05 14:16:43
@FredOverflow:基于抽象基类。 – 2012-08-05 14:54:00