I found code that looks like this:
typedef std::map<std::string, Example*(*)()> map_type;
and after searching for a while, I still can't figure out what the (*) operator does exactly. Anyone have any ideas?
In computer programming, a dereference operator, also known as an indirection operator, operates on a pointer variable. It returns the location value, or l-value in memory pointed to by the variable's value. In the C programming language, the deference operator is denoted with an asterisk (*).
Here, * is called dereference operator. This defines a pointer; a variable which stores the address of another variable is called a pointer.
When used with an already declared variable, the asterisk will dereference the pointer value, following it to the pointed-to place in memory, and allowing the value stored there to be assigned or retrieved.
It declares a pointer to a char pointer.
The parens here are use to impose precedence. The type
Example*(*)()
is a pointer to function returning pointer to Example
.
Without the parens you would have
Example**()
which would be a function returning pointer to pointer to Example
.
This is the syntax used to declare a pointer to a function (your case) or an array. Take the declaration
typedef Example* (*myFunctionType)();
this will make the line
typedef std::map<std::string, myFunctionType> map_type;
be exactly equivalent to the line you've given. Note that the difference between Example* (*myFunctionType)()
and Example* (*)()
is only that the name of the type has been omitted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With