.:: Bots United ::.

.:: Bots United ::. (http://forums.bots-united.com/index.php)
-   General Programming (http://forums.bots-united.com/forumdisplay.php?f=25)
-   -   C++ overriding methods using virtual (http://forums.bots-united.com/showthread.php?t=2023)

botmeister 21-06-2004 18:19

Re: C++ overriding methods using virtual
 
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 :)

Cheeseh 21-06-2004 18:36

Re: C++ overriding methods using virtual
 
I need a virtual function so I can override it with sub-class methods.

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

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 :P

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.

botman 22-06-2004 01:03

Re: C++ overriding methods using virtual
 
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?

Cheeseh 22-06-2004 02:25

Re: C++ overriding methods using virtual
 
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)

@$3.1415rin 22-06-2004 10:11

Re: C++ overriding methods using virtual
 
that should work when declaring the function virtual in the superclass .. well, i'll download that files and see :D

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

Cheeseh 22-06-2004 12:53

Re: C++ overriding methods using virtual
 
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 :/


All times are GMT +2. The time now is 05:54.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.