#### Archived

This topic is now archived and is closed to further replies.

# Game Object Orient Programming (GOOP)

This topic is 5533 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

This is really a standard OOP problem I'm sure, but it applies to games and GOOP sounds funny and might get this post some attention Here is the situation, lets say I have a Base class for my entities, whether its abstract or not doesn't really matter much, here is an example
class CBaseEntity
{
virtual void SayHello(){ cout << "Hello I'm a BaseEntity class!"; }
};

//Now I derive classes from it like so (and override the function)

class CTankEntity : public CBaseEntity
{
virtual void SayHello(){ cout << "Hello I'm a TankEntity class!"; }
};

class CMissileEntity : public CBaseEntity
{
virtual void SayHello(){ cout << "Hello I'm a MissileEntity class!"; }
};

To keep track of my entities I use a STL vecotr or something similar that holds pointers to CBaseEntiy, that way it can hold any enties derived from it.
vector <CBaseEntity*> EntityList;

I can add a few Entities to the vector...
EntityList.push_back( new CTankEntity() );
EntityList.push_back( new CTankEntity() );
EntityList.push_back( new CMissileEntity() );

Then somewhere in my program I want to call all the entites SayHello member function. So the simplest way I can think of is to do...
for ( int i = 0; i < EntityList.size(); i++)
EntityList[i]->SayHello();

However this presents my problem... Because the vector holds points to CBaseEntity the function call SayHello calls the CBaseEntity::SayHello() no matter what. I can solve this by typecasting the pointer, but since I have multiple types of derived Entites I'd need some big ugly switch code based on type. I've heard about RTTI, and that may be waht I need. But most books/pages I've read say to avoid RTTI at all costs... So what is the OOP way of _properly_ handling this situation? Thanks in advance. EIDT: Put all code in source tags [edited by - Illumini on June 16, 2003 6:08:38 PM]

##### Share on other sites
Cant you just make your base pure virtual?

##### Share on other sites
yea your structure is ok and it should work... try maybe to take off the virtual in CTank and CMissile...

##### Share on other sites
Well if structure is ok then something else must be wrong with my program LOL, but I''m just not sure. I''ll try a simple example like the one I posted just incase and see what hapens.

Reaper, from what I saw happening in my program it seems that it wouldstill call the pure virutal function thus causing the application to crash.

Sross, to my knowledge leaving virtual on the overridden function has no effect toher than for clarity. So I doubt removing it would help.

##### Share on other sites
yes virtual means dosent exists. remove virtual from derived classes.

##### Share on other sites
I''m not going to start throwing around misinformation since I''m by know means a C++ expert... But I''m fairly sure virtual has a handful of uses and leaving it on derived classes doe not sovle my problem. I''m testing right now tho

##### Share on other sites
Yay! Test it both my original example works (with or without virtuals on derived classes).

This means the bug must be related to something else in my program.... Greaaaat. LOL.

Thanks to everyone who posted here!

##### Share on other sites
I would have thought you would need virtual on all derived classes that define the function SayHello. At least that''s what I saw from my experience of doing something similar.

Omitting the virtual on tank and missile entities should mean that their SayHello function would never get called.

I''m confused..............

##### Share on other sites
As long as virtual is on the base class function it is automatically implied for all classes derived from it. virtual simply means the function can be overwritten in a derived class. Therefore it would make sense that if its virtual in the base class the virtualness must carry through all derived classes. You can still put virtual on the function in the derived class but you don''t need to.

Thats how I understand it from when I learned C++.

##### Share on other sites
Hi.

Given the code in your message (plus the ''public:'' keyword as the Hello method will be private (default is private for classes) in the code snipped) the array will in fact display ''Hello I''m a TankEntity class!'' and ''Hello I''m a MissileEntity class!''.

This works because the virtual keyword before the method declaration causes the compiler to build a ''virtual function pointer'' table. Even though the elements in the vector are accessed through a base class pointer, they were added as to the vector as derrived objects.

Steve

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
11

• 9
• 12
• 16
• 26
• 10
• ### Forum Statistics

• Total Topics
633768
• Total Posts
3013753
×