Couple of questions

Started by
0 comments, last by paulecoyote 19 years, 4 months ago
Hi I have just recieved the following question: The following line of code declares a pointer to a function type: typedef void (* MyFunctionPointer)( void * lpUserData ); Give two examples of situations where a variable of this type might be used. Are there any other ways of achieving the same results, and if so, what are the pros and cons of those alternatives? I have no idea why this might be used unless it has something to do with being able to code using the fucntion without actually knowing what thefucntions does (as in someones else is writing the function). And i guess a better way of doing it would be with a stardard function. I am sure i am wrong and would really like the correct answer as it has now been driving me mad for the last hour. Thanks Buzz
Advertisement
In other languages (like C#) this method of function calling is called a "delegate". The Win32API uses a similar thing in some functions called a "callback".

Basically it means the function that uses the pointer to the functions doesn't know or care what that function does, as long as it lives up to a contract.

For example EnumWindows uses it in the Win32API. It calls your function reference for each window it finds, and passes along information about that found window.

Some people use them in stacks to store state. Eg

-top-
[pause]
[playGame]
[mainMenu]
-bottom-

As long as all those functions have the same signature, the game engine loop need not know what it does, it just calls them. So for above the game is paused. Pop off the state

-top-
[playGame]
[mainMenu]
-bottom-

and game is in play again. Pop again and your at the main menu. From there you may push on the settingsMenu.
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.

This topic is closed to new replies.

Advertisement