Sign in to follow this  
rogierpennink

Problem with function pointer

Recommended Posts

Hello everyone. I have a bit of a problem with passing function pointers as an argument to a function. That is, the declaration is no problem, but when using the 'passed' function within the function it gives me an error saying: error C2064: term does not evaluate to a function The entire function can be seen below, but before I'll copy 'n paste the code I'll put it in a context as to explain what I'm trying to do. Basically I'm adding functions to a "3d engine" as I continue learning Direct3D. Everytime I've learned something new I think of how I would ease the process and add it to my class. The function below is the InitGameLoop function, which will start the game loop and takes a function pointer as argument. The function the user provides is the 'loop content'.
int u3D::IUniverse3DInterface::U3DInitGameLoop ( bool (*LoopFunc), MSG ** u_msg )
{
	// Initialize windows MSG struct
	MSG msg;	

	// Start the loop
	while ( true ) {

		// Check for windows messages
		if ( PeekMessage ( &msg, NULL, 0, 0, PM_REMOVE ) > 0 )
		{
			// If the user defined the wish to receive the message as well
			if ( u_msg != 0 ) {
				*u_msg = &msg;
			}

			TranslateMessage ( &msg );
			DispatchMessage ( &msg );

		} else {

			// Do the specified game loop
			if ( LoopFunc() == false ) {  // This gives the error
				break;
			}

		}
	}
	
	return msg.wParam;
}

I hope this is enough information, if not, I'll gladly provide more ;-)

Share this post


Link to post
Share on other sites
Ahh, thanks, you are absolutely right and it worked fine :-) In addition - do you always have to add those parentheses when you're dealing with function pointers, or is it just in specific cases like this one?

Share this post


Link to post
Share on other sites
Quote:
Original post by rogierpennink
Ahh, thanks, you are absolutely right and it worked fine :-) In addition - do you always have to add those parentheses when you're dealing with function pointers, or is it just in specific cases like this one?


The parentheses tells the compiler that the type is a function pointer that has a variable number of parameters. Without the parenthesis, you wouldn't be able to specify the parameters types, and the compiler would not be able to ensure type safety.

Regards,

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