views:

284

answers:

9

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;
+9  A: 

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.

Billy ONeal
A: 

This is a pointer to a function that takes two references to ints and returns void.

SLaks
A: 

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.

Uri
+2  A: 

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);
Gareth Stockwell
A: 

It's a function pointer to a function that takes 2 integers as arguments and returns void. x must be a function name.

Charles Ma
It's taking integer reference arguments, not just plain integers...
kusma
Actually, they are references to integers, not integers (No, I am not the downvoter)
Billy ONeal
Actually, x could be another function pointer.
Noah Roberts
+2  A: 

Function pointer.

http://www.newty.de/fpt/intro.html#what

^ Okay source for a beginner. :-)

Deep-B
This is what I used :]
+14  A: 

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:

  • a pointer to a function ((*…))
  • returning void
  • and taking 2 arguments: int&, int&.
KennyTM
+1 for translating to the more common `typedef` syntax.
Billy ONeal
+1 for showing typedef syntax
Gareth Stockwell
A: 

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 = ...;
sth
A: 

Wikipedia page with a few links on the subject: http://en.wikipedia.org/wiki/Function_pointer

jakar