开发者

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) (i开发者_C百科nt&,int&) = x;


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&.


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.


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);


Function pointer.

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

^ Okay source for a beginner. :-)


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


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.


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


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 = ...;


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜