Virtual Method Question
by Dumbledore · in Torque Game Engine · 05/24/2008 (6:08 am) · 5 replies
Hello, I am terribly sorry for posting more of a C++ related question here, but I have sought wisdom from purely C++ guru's and they too do not understand the following:
In the RTSSK there is code that looks like this:
VisManager.cc
I added a virtual method called virtual SceneObject::getTeam() to SceneObject. RTSUnit derives from SceneObject (eventually up the heirarchy). I used to think this was the result of type slicing, but I don't think so anymore since the dynamic_cast fixes it.
Can anyone explain what is happening here that causes the virtual base method to be called on a RTSUnit* ?
Here is where RTSUnit* get's added:
Thank you so much for you time. I have literally spent hours upon hours trying to understand this.
In the RTSSK there is code that looks like this:
VisManager.cc
void VisManager::processClient()
{
if ( !mDirty ) // if it is not dirty that means it doesn't need to update.
return;
PROFILE_START(VisManager_Process_Client);
RTSConnection* conn = RTSConnection::getServerConnection();
for (U32 i = 0; i < mObjectList.size(); i++)
{
RTSUnit* unit = dynamic_cast<RTSUnit*>(mObjectList[i]);
SceneObject* virtualBase = (SceneObject*)mObjectList[i];
virtualBase->getTeam(); // This will call virtual SceneObject::getTeam()
unit->getTeam() // This will call RTSUnit::getTeam()I added a virtual method called virtual SceneObject::getTeam() to SceneObject. RTSUnit derives from SceneObject (eventually up the heirarchy). I used to think this was the result of type slicing, but I don't think so anymore since the dynamic_cast fixes it.
Can anyone explain what is happening here that causes the virtual base method to be called on a RTSUnit* ?
Here is where RTSUnit* get's added:
void VisManager::addObject(SceneObject* unit)
{
mObjectList.addObject(unit);
}Thank you so much for you time. I have literally spent hours upon hours trying to understand this.
#2
Wow, I think it is because I did not make getTeam() "const" in SceneObject. Let me see...
05/24/2008 (7:03 am)
class SceneObject : public NetObject
{
virtual S32 getTeam() { AssertISV(false, "Should never call this method."); return 0; };
}
class RTSUnit : public Player
{
getTeam() const { return mTeam }
}Wow, I think it is because I did not make getTeam() "const" in SceneObject. Let me see...
#3
05/24/2008 (7:11 am)
Yep, that was the problem. How silly of me.
#4
05/24/2008 (8:07 am)
Congratulations. :)
#5
05/24/2008 (8:30 am)
Quote:This is an example of the message board gods demanding tribute. It was not that your friends, the C++ guru's, didn't know the answer, or that you didn't know the answer. It was the message board gods demanding tribute in the form of a thread, in which the author posts a question, and then 5-or-so minutes later posts something to the effect of, "Whoops...never mind." It's what they live on. It's their Ambrosia.
Posted: May 24, 2008 07:03
...
Posted: May 24, 2008 07:11
Torque Owner Stefan Lundmark