Steven ZA
Steven ZA

Reputation: 115

what is Base*(*)()

Can someone please explain the Base*(*)() for me as in:

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

And how would one return it from a function?

I presume it is a function pointer, a Base* is returned, but what is this (*).

I found this in the following SO post Is there a way to instantiate objects from a string holding their class name?

Thanks

Upvotes: 2

Views: 148

Answers (2)

CB Bailey
CB Bailey

Reputation: 792129

Base* (*)() is a type: pointer to function returning Base*. The * means that it is a pointer and the () are used to to override the precedence to ensure that the pointer applies to the function itself and not the return type.

You can return it from a function by returning the name of a function of the appropriate type.

E.g.

Base* f();

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

Upvotes: 7

Kerrek SB
Kerrek SB

Reputation: 477150

It's the type of a function pointer of a function of signature Base*():

Base * foo();

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

Or in your case:

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

To invoke:

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

Upvotes: 4

Related Questions