.:: 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
Re: C++ overriding methods using virtual
Old
  (#11)
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

Quote:
Originally Posted by Cheeseh
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...
I'd say you have not sorted the problem out, but instead found a work around.

In general, you cannot get away without the use of constructors/destructors if you want to use some of the more advanced features provided with classes.

It is possible there's a problem with the MVC compiler, but more likely the problem lies elsewhere, since it is absolutely neccessary that constructors/destructors work flawlessly, at least when used as intended (or expected).

Cheeseh, perhaps I do not understand what is going wrong. Can you show me another example, or give me more details? Does the problem go away without the use of the virtual function?

ps: I'd like to see your "normal" code that does not work as expected, not the code with the attempts to get around the problem - thanks


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

Last edited by botmeister; 21-06-2004 at 19:20..
  
Reply With Quote
Re: C++ overriding methods using virtual
Old
  (#12)
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 need a virtual function so I can override it with sub-class methods.

Sure I worked around the problem, that's good enough

It was because I had that constructor call to it's super class inside the sub class' constructor, it mucked up the class in some way (almost turning it into the superclass class...).

I guess that the definition that you showed earlier...

CSubClass :: CSubClass () : CSuperClass()
{
...
}

was a better way of doing it but I haven't tried that since I'd rather stick to my current method... but whenever I need to do this again, I'll try it like that

The normal code is just a list of bot commands, a list of "CBotCvar"'s (which is the super class) and each subclass is a unique command... e.g. bot_menu (shown earlier). It has it's own action() function which does its stuff.

The super classes action method is virtual and it does nothing. So that when I look through the list and call the bot_menu "CBotCvar" action() method, it should call CBotMenuCommand's action() to do its business.

I need the action function in the superclass so I can compile it! And I need it to be virtual so that it will call the appropriate action() method of the subclass if I wrote one for the subclass.

Last edited by Cheeseh; 21-06-2004 at 19:37..
  
Reply With Quote
Re: C++ overriding methods using virtual
Old
  (#13)
botman
Super Moderator
 
Status: Offline
Posts: 280
Join Date: Jan 2004
Location: Plano, TX
Default Re: C++ overriding methods using virtual - 22-06-2004

In your original post, you said...

" gBotGlobals.m_CurrentHandledCvar is a "CBotCvar*" variable."

Which means you are declaring pointers to the base class and then trying to call virtual functions from the derived class (which won't happen without you doing some run-time dynamic casting on the pointer).

Why didn't you just make 'm_CurrentHandledCvar' a pointer to 'CBotMenuCommand' instead of a pointer to the base class?
  
Reply With Quote
Re: C++ overriding methods using virtual
Old
  (#14)
Cheeseh
[rcbot]
 
Cheeseh's Avatar
 
Status: Offline
Posts: 361
Join Date: Dec 2003
Location: China
Default Re: C++ overriding methods using virtual - 22-06-2004

oh man... the confusion

The current "CVAR" is updated when a player enters a client command of a valid bot command. The valid bot command will be a pointer to a subclass instance of CBotCvar.

All the valid commands are held in a list of CBotCvar pointers. I need this as this is the only way to have a heterogeneous list of objects (eg. vector<CBotCvar*> m_BotCvars for the list).

I cannot make a list of completely different objects using OO methods, I don't think so anyway.

When the CBotCvar is matched by the cvarname (a string held in the CBotCvar class) it will call the action method. If the retrieved instance is derived from the CBotCvar class it will call it's own action method if it exists, so it will do it's own stuff. (e.g. If the retrieved CBotCvar* pointer from the list is derived from CBotMenuCommand class, it will call CBotMenuCommand 's action() method)

I'm sure this is the basic stuff of OO dynamic dispatching... certainly in Java, much stuff in Swing is done like this.

believe me, it works!!!

(I've updated my bot source on my site with the stuff... bot.h/bot_commands.h/bot_commands.cpp has the stuff)

Last edited by Cheeseh; 22-06-2004 at 03:34.. Reason: ohh my sore head...
  
Reply With Quote
Re: C++ overriding methods using virtual
Old
  (#15)
@$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 - 22-06-2004

that should work when declaring the function virtual in the superclass .. well, i'll download that files and see

So that files that are there at the moment do _not_ work as supposed to, right ?


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

They actually DO work as supposed to... If you put in that constructor thingy where it called the super class in each subclasses constructor, then it will muck it up entirely :/

Last edited by Cheeseh; 22-06-2004 at 13:57..
  
Reply With Quote
Reply


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

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