Sign in to follow this  
buzzby

Couple of questions

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this