Jump to content
  • Advertisement
Sign in to follow this  

void* to functor casting???

This topic is 4707 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, I've got a little function pointer conversion problem. Here's a simplified version of what I'm doing:
// Game Phase Funtors
int		(*DoIntro)();
int		(*DoFrame)();
int		(*DoOutro)();

int SetFlag(int ID, void *value)
{
	switch( ID )
	{	
		case FLAG_FRAMEFUNC:
			DoFrame = (???)value;	// how do I cast this?
			break;
		case FLAG_TITLETEXT:
			m_TitleText = (char*)value;
                        break;
	}
	return 0;
}

int MyFrameFunction()
{
	Render();
	Input();
	Update();
	
	return 0;
}

int WinMain()
{
	SetFlag(FLAG_FRAMEFUNC, MyFrameFunction);
	return 0;
}




Alright, I know there are other ways of doing this, but I like this approach the best. And I don't think switches are that bad either [smile]. PS: Does anyone know how to change the win32 titlebar text at runtime? [help] [Edited by - The Forgotten Mindset on June 29, 2005 9:20:00 PM]

Share this post


Link to post
Share on other sites
Advertisement
[EDIT - removed reinterpret_cast example]

You can change the title bar as soon as you have a window handle-- maybe when processing WM_ACTIVATEAPP;


SetWindowText(HWND,LPCTSTR);

Share this post


Link to post
Share on other sites
Example, calling value as a function taking no arguments and returning void: (*(void(*)(void))value)();

Beware that function pointers aren't guarenteed to be the same size as data pointers in cause you care about portability. As an example:

void (*function)(void) = reinterpret_cast<void(*)(void)>(pointer);

This gives me 'error: ISO C++ forbids casting between pointer-to-function and pointer-to-object' when compiling with GCC.

Share this post


Link to post
Share on other sites
Thanks, I'll give it a try. I'm confused now :/ I'm not used to this kind of c++. I've used function pointers before, but not like this.

But the SetWindowText function didn't work for me for some reason, and it was the correct window handle. Oh, I don't think my screen was updating.

Share this post


Link to post
Share on other sites
From MSDN:


The SetWindowText function changes the text of the specified window's title bar
(if it has one). If the specified window is a control, the text of the control is
changed. However, SetWindowText cannot change the text of a control in another
application.

BOOL SetWindowText(
HWND hWnd, // handle to window or control
LPCTSTR lpString // address of string
);



Sorry, don't know what to say [smile]

Share this post


Link to post
Share on other sites
What type is DoFrame? If value is a function pointer, then your function signature is wrong:

int SetFlag(int ID, void (*lpfn)())

Share this post


Link to post
Share on other sites
Quote:
Original post by smr
What type is DoFrame? If value is a function pointer, then your function signature is wrong:

int SetFlag(int ID, int (*lpfn)())

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!