Reputation:
What is this syntax for in C++? Can someone point me to the technical term so I can see if I find anything in my text?
At first I thought it was a prototype but then the =
and (*fn)
threw me off...
Here is my example:
void (*fn) (int&,int&) = x;
Upvotes: 3
Views: 532
Reputation: 1061
Wikipedia page with a few links on the subject: http://en.wikipedia.org/wiki/Function_pointer
Upvotes: 0
Reputation: 229914
It's a function pointer variable that gets initialized to the stuff to the right of =
.
The function type could be written like this:
typedef void func_t(int&,int&);
The function pointer than would be:
typedef func_t *fn_t;
With these definitions the variable declaration would be much clearer:
fn_t fn = ...;
Upvotes: 0
Reputation: 523764
It can be rewritten to
typedef void (*T) (int&, int&);
T fn = x;
The 2nd statement is obvious, which should have solved that = x;
question. In the 1st statement, we make T
as a synonym as the type void(*)(int&, int&)
, which means:
(*…)
)void
int&, int&
.Upvotes: 14
Reputation: 1344
Function pointer.
http://www.newty.de/fpt/intro.html#what
^ Okay source for a beginner. :-)
Upvotes: 2
Reputation: 49221
It's a function pointer to a function that takes 2 integers as arguments and returns void. x must be a function name.
Upvotes: 0
Reputation: 3122
This declares and initializes a function pointer.
The name of the variable is fn
, and it points to a function with the following signature:
void pointedToFunction(int&, int&)
The variable fn
is initialized to the value contained in x
.
The pointed-to function can be called using the following syntax:
int a;
int b;
(*fn)(a,b);
This is equivalent to
int a;
int b;
pointedToFunction(a,b);
Upvotes: 2
Reputation: 89859
That seems like a function pointer to a method that takes two integer references and does not return anything. The pointer will be named fn. You are assigning it to the address of x, which is hopefully a function that matches this description.
Upvotes: 0
Reputation: 888213
This is a pointer to a function that takes two references to int
s and returns void
.
Upvotes: 0
Reputation: 106609
That is a function pointer to a function taking two int
reference parameters, which returns nothing. The function pointer is called fn
and is being assigned the value in x
.
Upvotes: 9