What is a single asterisk inside parent elements of "(*)" in C ++?

I found code that looks like this:

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

and after some searching, I still can't figure out what the (*) operator does. Does anyone have any ideas?

+4
source share
4 answers

Paranas are used here to impose priority. A type

Example*(*)()

- a pointer to a function that returns a pointer to Example.

Without partners you will have

Example**()

which will be a function that returns a pointer to a pointer to Example.

+7
source

This is the syntax used to declare a pointer to a function (your case) or an array. Take an ad

typedef Example* (*myFunctionType)();

it will make a line

typedef std::map<std::string, myFunctionType> map_type;

, . , Example* (*myFunctionType)() Example* (*)() , .

+5

This is a function pointer declaration .

This typedef means that std :: map maps strings to function pointers that receive void and return Example * .

You can use it as follows:

#include <string>
#include <map>

typedef  int Example;

Example* myExampleFunc() {
    return new Example[10];
};

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

int main() {

    map_type myMap;
    // initializing
    myMap["key"] = myExampleFunc;

    // calling myExampleFunc

    Example *example = myMap["key"]();

    return 0;
}
0
source

With typedefs, you can create complex pointer types much more complicated in C ++ 11:

template<typename T> using ptr = T*;

typedef std::map<std::string, ptr<Example*()>> map_type;
-1
source

Source: https://habr.com/ru/post/1540272/


All Articles