Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the single asterisk inside parentheses "(*)" do in C++?

Tags:

c++

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?

like image 727
autobahn Avatar asked May 12 '14 18:05

autobahn


People also ask

What does the asterisk do in C?

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 (*).

What does * After a variable mean in C++?

Here, * is called dereference operator. This defines a pointer; a variable which stores the address of another variable is called a pointer.

What does asterisk after a variable mean in C?

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.

What does 2 asterisks mean in C?

It declares a pointer to a char pointer.


2 Answers

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.

like image 50
David Heffernan Avatar answered Sep 20 '22 03:09

David Heffernan


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.

like image 45
cmaster - reinstate monica Avatar answered Sep 20 '22 03:09

cmaster - reinstate monica