Member Function Pointer Mayham
EDIT: Skip this and read my reply. It gives a much more clear scenerio.
I need to make use of a function pointer to a member function. I am running into many issue's with this.
The problem is I am trying to store a function pointer which is a member of class 'State' but I am passing in a function from class 'GameState' which derives from 'State'.
Here is the snippets to best describe my dilema:
PHP Code:
//An abstract State class which contains this virtual method
/**
* Handles messages for the gameWindow
*/
virtual LRESULT CALLBACK messageHandlerWindow(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) = 0;
//Here is a class which derives from the State class and overrides the method
/**
* Handles messages for the gameWindow
*/
LRESULT CALLBACK GameState::messageHandlerWindow(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
//Here is a GraphicLayer class which needs to be able to
//take in a function pointer to its messageHandlerWindow(..)
//This messageHandler has to be generic because there are more states
//than just the gameState seen above.
//Member in GraphicLayer to store the func pointer
//non-static State specific message handler
LRESULT (__stdcall State::*m_pMsgHandler)(HWND, UINT, WPARAM, LPARAM);
/**
* Attach a STATE specific message handler to the window
* The static message handler does nothing but pass its messages to this
* @param msgHandler - A function pointer to the msgHandler
*/
void GraphicLayer::attachWindowMessageHandler(LRESULT (__stdcall State::*msgHandler)(HWND, UINT, WPARAM, LPARAM))
{
m_pMsgHandler = msgHandler;
}
The errors I get just keep running me in circles.
error C2664: 'GraphicLayer::attachWindowMessageHandler' : cannot convert parameter 1 from 'LRESULT (__stdcall GameState::* )(HWND,UINT,WPARAM,LPARAM)' to 'LRESULT (__stdcall State::* )(HWND,UINT,WPARAM,LPARAM)'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Also, how would I write the return for that... It seems rather complex:
PHP Code:
/**
* Returns the message handler which was attached to the graphicLayer
* @return msgHandler - The pointer attached to the graphic layer
*/
LRESULT (__stdcall State::*GraphicLayer::getAttachedWindowMessageHandler())(HWND, UINT, WPARAM, LPARAM)
{
return m_pMsgHandler;
}
Re: Member Function Pointer Mayham
Lemme make life simplier on everyone:
class A
{
virtual void foo() = 0;
}
class B : public A
{
void foo() {};
}
class C : public A
{
void foo() {};
}
I need to be able to store the member function pointer to foo() in one place, but it must be able to store the B:foo() or the C:foo() in one pointer. Since they both derive from A:foo() I figure this would be easy. However some reading has shown this to be either impossible or extremely complex.
Re: Member Function Pointer Mayham
The problem doesn't make sense. The point of virtual functions is that the correct one is called automatically. So you shouldn't store a pointer to the member function, you should store just a pointer to a State object and call the virtual on that.