|
-
Feb 2nd, 2006, 05:56 PM
#1
Thread Starter
PowerPoster
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;
}
Last edited by Halsafar; Feb 2nd, 2006 at 06:19 PM.
"From what was there, and was meant to be, but not of that was faded away." - - Steve Damm
"The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm
"When you do things right, people won't be sure if you did anything at all." - - God from Futurama
-
Feb 2nd, 2006, 06:19 PM
#2
Thread Starter
PowerPoster
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.
"From what was there, and was meant to be, but not of that was faded away." - - Steve Damm
"The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm
"When you do things right, people won't be sure if you did anything at all." - - God from Futurama
-
Feb 3rd, 2006, 09:12 AM
#3
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|