View Single Post
C++ overriding methods using virtual
Old
  (#1)
Cheeseh
[rcbot]
 
Cheeseh's Avatar
 
Status: Offline
Posts: 361
Join Date: Dec 2003
Location: China
Default C++ overriding methods using virtual - 20-06-2004

alright, call me a blind man or getting too old for this now... but...

I have a class which is a subclass of another
the super class has a virtual method called action()
and the sub class has a non-virtual method also called action() with the same parameters.

Now, when I invoke the action() method on an instance of the subclass, it should call the overidden method (the one in the subclass) and NOT the one in the super class, right?

Well not in this situation, WHY!!?

My SuperClass...
Code:
class CBotCvar
{	
public:
...

	virtual eBotCvarState action ( CClient *pClient, const char *arg1, const char *arg2, const char *arg3, const char *arg4 )
	{
		return BOT_CVAR_ERROR;
	}

...
};
My SUB class...
Code:
class CBotMenuCommand : public CBotCvar
{
public:
    CBotMenuCommand ()
    {
        this->CBotCvar::CBotCvar("bot_menu",8,FALSE);
    }
	
	void showHelp ( edict_t *pEntity )
	{
		BotMessage(pEntity,0,"bot_menu command help");
	}
	
	eBotCvarState action ( CClient *pClient, const char *arg1, const char *arg2, const char *arg3, const char *arg4 );
};
its method...

Code:
eBotCvarState CBotMenuCommand :: action ( CClient *pClient, const char *arg1, const char *arg2, const char *arg3, const char *arg4 )
{
	//edict_t *pEntity;
	
	if ( pClient == NULL )
	{
		BotMessage(NULL,0,"This command can only be used on the client");
		return BOT_CVAR_ERROR;
	}
	
	//pEntity = pClient->GetPlayer();
	
	gBotGlobals.m_Menus[BOT_MENU_BOT_MAIN].Render(pClient);
	
	return BOT_CVAR_ACCESSED;
}
Calling the method...

Code:
iState = gBotGlobals.m_CurrentHandledCvar->action(pListenServerClient,arg1,arg2,arg3,arg4);
gBotGlobals.m_CurrentHandledCvar is a "CBotCvar*" variable.

Now even though it's a CBotCvar variable it should call the overridden one, because I add a new CBotMenuCommand() to the list of commands which is taken as a CBotCvar* so I can have a heterogeneous list. So it should call the action() method of CBotMenuCommand... BUT it doesn't Argh!

It always calls the crappy method in the super class which does nothing and just returns an error!

Can any one help? I know in Java this would work fine... but C++ is starting to annoy me now :'(

Last edited by Cheeseh; 21-06-2004 at 01:32.. Reason: code error
  
Reply With Quote