什么是Base *(*)()

时间:2012-08-05 13:49:41

标签: c++ templates stl

有人可以为我解释 Base *(*)(),如:

typedef std::map<std::string, Base*(*)()> map_type;

如何从函数中返回它?

我认为它是一个函数指针,返回一个Base *,但这是什么(*)。

我在以下SO帖子Is there a way to instantiate objects from a string holding their class name?

中找到了这个

由于

2 个答案:

答案 0 :(得分:7)

Base* (*)()是一个类型:指向返回Base*的函数的指针。 *表示它是一个指针,()用于覆盖优先级,以确保指针适用于函数本身,而不是返回类型。

您可以通过返回相应类型的函数名称从函数返回它。

E.g。

Base* f();

Base* (*g())()
{
    return f;
}

答案 1 :(得分:4)

它是签名Base*()函数的函数指针的类型:

Base * foo();

Base * (*fp)() = &foo;

或者在你的情况下:

map_type callbacks;
callbacks["Foo"] = &foo;

要调用:

Base * p = callbacks["Foo"]();  // same as "p = foo();"
相关问题