anon235370
anon235370

Reputation:

Cryptic C++ "thing" (function pointer)

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

Answers (9)

jakar
jakar

Reputation: 1061

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

Upvotes: 0

sth
sth

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

kennytm
kennytm

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:

  • a pointer to a function ((*…))
  • returning void
  • and taking 2 arguments: int&, int&.

Upvotes: 14

Deep-B
Deep-B

Reputation: 1344

Function pointer.

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

^ Okay source for a beginner. :-)

Upvotes: 2

Charles Ma
Charles Ma

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

Gareth Stockwell
Gareth Stockwell

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

Uri
Uri

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

SLaks
SLaks

Reputation: 888213

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

Upvotes: 0

Billy ONeal
Billy ONeal

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

Related Questions