.:: Bots United ::.  
filebase forums discord server github wiki web
cubebot epodbot fritzbot gravebot grogbot hpbbot ivpbot jkbotti joebot
meanmod podbotmm racc rcbot realbot sandbot shrikebot soulfathermaps yapb

Go Back   .:: Bots United ::. > Developer's Farm > General Programming
General Programming Help others and get yourself helped here!

Reply
 
Thread Tools
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
Re: C++ overriding methods using virtual
Old
  (#2)
sfx1999
Member
 
sfx1999's Avatar
 
Status: Offline
Posts: 534
Join Date: Jan 2004
Location: Pittsburgh, PA, USA
Default Re: C++ overriding methods using virtual - 20-06-2004

Is it a problem with function overloading? I mean you have one as a virtual and one as a non-virtual.
  
Reply With Quote
Re: C++ overriding methods using virtual
Old
  (#3)
@$3.1415rin
Council Member, Author of JoeBOT
 
@$3.1415rin's Avatar
 
Status: Offline
Posts: 1,381
Join Date: Nov 2003
Location: Germany
Default Re: C++ overriding methods using virtual - 20-06-2004

Pretty difficult to understand other ppls oo coding problems I have to admit

If you have an instance of your superclass, but a pointer to the base class, and the function is defined virtual in the base class, the function of the superclass is called, even if it may not be virtual in the super class. If you wanna explicitly call the one of the base class, you need to add an additional scope or however this operator is called. so pointer->CBotCvar::action would call the base class function.


  
Reply With Quote
Re: C++ overriding methods using virtual
Old
  (#4)
Cheeseh
[rcbot]
 
Cheeseh's Avatar
 
Status: Offline
Posts: 361
Join Date: Dec 2003
Location: China
Default Re: C++ overriding methods using virtual - 21-06-2004

I want to implicitly call the sub classes method, which should be done by default! But it's not, I am wondering if I am invoking the method correctly or have specified the correct parameters okay etc. There is no parameter overloading...

even if they are both defined as virtual they don't work

Last edited by Cheeseh; 21-06-2004 at 01:18..
  
Reply With Quote
Re: C++ overriding methods using virtual
Old
  (#5)
Cheeseh
[rcbot]
 
Cheeseh's Avatar
 
Status: Offline
Posts: 361
Join Date: Dec 2003
Location: China
Default Re: C++ overriding methods using virtual - 21-06-2004

lol!

Found my problem...

Anyone know how to call super class constructor WITHOUT messing up the bloody class???

Check this test program I made...

Code:
#include <stdio.h>

class CSuperClass
{
public:
	virtual void override_me ()
	{
		printf("WRONG METHOD CALLED...WTF???\n");
		return;
	}
};

class CSubClass : public CSuperClass
{
public:

	// mess_up is true, it will call the super classes constructor on me
	// it isn't very useful in this case, but is very useful in other cases
	CSubClass(bool mess_up)
	{
		if ( mess_up )
			this->CSuperClass::CSuperClass();
	}

	void override_me ()
	{
		printf("CORRECT METHOD CALLED...WOOOT!!!\n");
		return;
	}
};

int main ( int argc, char **argv )
{
	CSuperClass *pInstance1 = new CSubClass(false);
	CSuperClass *pInstance2_messed = new CSubClass(true);

	pInstance1->override_me();

	pInstance2_messed->override_me();

	delete pInstance1;
	delete pInstance2_messed;

	return 1;
}
You'll see that when you initialise when calling the super classes constructor messes up the class for some reason or another...

Now I need to make my own method to initialise instead of calling the super class constructor....

...
  
Reply With Quote
Re: C++ overriding methods using virtual
Old
  (#6)
stefanhendriks
RealBot Author
 
stefanhendriks's Avatar
 
Status: Offline
Posts: 3,088
Join Date: Nov 2003
Location: Netherlands
Default Re: C++ overriding methods using virtual - 21-06-2004

hmm yes thats bummer. I don't like constructors/destructors anyways. I always used my own init() functions in a class. I wonder how this will work if both classes have the same init() functions..


Author of RealBot, "Arrakis" and "Dune 2 - The Maker" | co-Founder of Bots-United | Fundynamic | Blog | E-Mail me
  
Reply With Quote
Re: C++ overriding methods using virtual
Old
  (#7)
botmeister
Ex-Council Member
 
botmeister's Avatar
 
Status: Offline
Posts: 1,090
Join Date: Nov 2003
Location: Canada
Default Re: C++ overriding methods using virtual - 21-06-2004

Maybe I've stayed up too late again, but my understanding of classes is as follows:

The derived class constructor must call the base class constructor before anything is initialized in the derived class. If you have not declared any constructors, the compiler will insert default constructors for you and call them in the proper order. In your case, you have defined a declared constructor, but the same rule applies - the base class constructor always get called before the derived class constructor (the rule makes perfect sense if you think about it).

If you declare a constructor in a derived class, you must call the base class constructor as follows which holds the initilization rule in place:

CSubClass::CSubClass(bool mess_up) : CSuperClass()
{


if ( mess_up )
this->CSuperClass
::CSuperClass();
}

In your case you did not specify the base class constructor, but the compiler will call it for you anyway before CSubClass is called.

When mess_up evaluates to true, your code is attempting to call the base class constructor twice and improperly (which is why you are resorting to using this)!
You cannot prevent a class from executing its constructor, and if you manage to do it, your class will not initialize properly.

One more note about all this, the base class constructor should not make use of variables from the derived class (via a virtual function call) because they won't be initialized until after the base class constructor call is completed. Generally, the compiler will initialize these variables to zero (or null) beforehand anyway, but this is not a rule that can be relied on.



Maker of the (mEAn) Bot.Admin Manager

"In theory, there is no difference between theory and practice. But, in practice, there is." - Jan L.A. van de Snepscheut
  
Reply With Quote
Re: C++ overriding methods using virtual
Old
  (#8)
Whistler
Summoner
 
Whistler's Avatar
 
Status: Offline
Posts: 1,499
Join Date: Feb 2004
Location: Mist Village
Default Re: C++ overriding methods using virtual - 21-06-2004

I think it's a bug of MSVC since that's actually INVALID, and that test program doesn't compile in GNU GCC and Borland C++ at all:
Quote:
C:\>gcc 1.cpp
1.cpp: In constructor `CSubClass::CSubClass(bool)':
1.cpp:22: parse error before `;' token

C:\>bcc32 1.cpp
Borland C++ 5.6 for Win32 Copyright (c) 1993, 2002 Borland
1.cpp:
Error E2316 1.cpp 22: 'CSuperClass' is not a member of 'CSuperClass' in function
CSubClass::CSubClass(bool)
Warning W8057 1.cpp 45: Parameter 'argc' is never used in function main(int,char * *)
Warning W8057 1.cpp 45: Parameter 'argv' is never used in function main(int,char * *)
*** 1 errors in Compile ***
also I think if the constructor of base class if NOT virtual it will automatically get called before the constructor of sub class gets called.
  
Reply With Quote
Re: C++ overriding methods using virtual
Old
  (#9)
Cheeseh
[rcbot]
 
Cheeseh's Avatar
 
Status: Offline
Posts: 361
Join Date: Dec 2003
Location: China
Default Re: C++ overriding methods using virtual - 21-06-2004

Thanks anyway, but I sorted the problem anyway by making my own setup() ( initialise method) instead of using the constructors, (as stefan said too :p). But it's a bizzare problem anyway...

In my bot code, I tried the same as...

CSuperClass::CSuperClass() instead of this->CSuperClass::CSuperClass()

And a problem arises there when you want to initialize stuff, it doesn't actually change the stuff you want to initialize...
  
Reply With Quote
Re: C++ overriding methods using virtual
Old
  (#10)
@$3.1415rin
Council Member, Author of JoeBOT
 
@$3.1415rin's Avatar
 
Status: Offline
Posts: 1,381
Join Date: Nov 2003
Location: Germany
Default Re: C++ overriding methods using virtual - 21-06-2004

with that virtual functions, I guess you somehow got the sense of them wrong. a virtual function from a super class where the function exists in a subclass will never be called, unless you declare it explicitly like stated above. the whole virtual stuff wouldnt make much sense if you couldnt have a pointer to a base class and then without knowing what type of element it is, calling that function. depending if the instance is some subclass with its own function, or only the super class, the appropriate function will be called.

and it's also maybe a naming problem here. So you call e.g. all those BASE class in the HLSDK mainly superclasses, derived classes subclasses, right ? There is a lot of naming differences around, so that's maybe a problem too

and about constructors : yes, you have to get clear to see when which constructor gets called, but then it's advisable to use them to avoid initializing twice or even more. and the calling of constructors with this type of class config makes sense, since you want only to initialize the member variables. Sometimes it's getting a bit scary when you have no default ctor, but well, then you just have to do it via a : and then the base constructor with the arguments.

like this e.g. :
Code:
class CBV_HLDM_HideReload:public CBehaviour,CCallback_onPathCreation,CCallback_onReachedDestination{

public:

CBV_HLDM_HideReload(CBaseBot *pBot):CBehaviour(pBot){strcpy(m_szName,"CBV_HLDM_HideReload");}


  
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles.com