Skip to main content
GameDev.net gamedev.net
🔒 Locked

LinkList-Inheritance question

Started by SkinnyM Jan 8, 2004 at 3:27 PM 3 replies 500+ views
Original Post
SkinnyM
SkinnyM
Hello, Right now i am planning how i am going to store the data for my objects in my engine. I think i have come up with a way by using inheritance to use a link list to do what i want. When i do this, i want to be able to name my objects like CTerrain, or CCrazyGuyWithGuy, but in my link list , i cant possibly say CTerrain Instance, or CCrazyGuyWithGun Instance, because i dont know if it is terrain or crazy guy. So i was thinking to use inheritance and add it on to each object i create, something like CObject, that would have virtual functions in it, like Update, initalize, and destroy. And in my list list, i would just do CObject Instance; and then do something like Terrain = (CTerrain)Instance; then Terrain->Update() do what ever. Would that work, or wwould i have to use something like templates? I dont know much about them but i think this might be a scenario for them. Or if you have other ideas about how i can go store my objects like this, i am all ears Thanks for any help "What we do in life, echos in eternity" -- Gladiator
"What we do in life, echos in eternity" -- Gladiator
Kamil
Kamil
If you are using virtual functions you don''t have to make type casting. You can call :
CObject *object =...
object->Update();

insted of "Terrain = (CTerrain)Instance; then Terrain->Update()"

You have to cast if you want to call funtion what is unique in specyfic class like for example Terrain->GetColorOfGrass() or whatever oh and you will need to know what you have under your object pointer. So you will have to make virtual function like object->WhoAmI(); to make cast

sorry for my english
SkinnyM
SkinnyM

Thanks for the reply.

So what you are saying is that:

CObject *object = CAnyClassWithInheritedValuesFromCObject;
object->Update();

To say it again, with virtual functions, i can just use the base class that was used in inheritance and then use the base classs to repesent the bigger class? Maybe that doesnt sound right, so i will try to put it in code.

class CTerrain : public CObject
{
...virtual functions replaced here with real ones

void LetsDoIt(); //Not taking place of virtual
};

CTerrain Terrain;
CObject *Object = Terrain;

And then, i can use any function i declared in CTerrain like :

Object->LetsDoIt();

even thought LetsDoIt() isnt declared in CObject?

I never knew you could do that, i always assumed this wouldnt work. Hopefully it does.

Can you still clarify for me?
"What we do in life, echos in eternity" -- Gladiator
Kamil
Kamil
Sorry if I missguided you, but this what you are saying will not work...

First of all virtual functions works only for pointers, so insted of
CObject *object = CAnyClassWithInheritedValuesFromCObject;
You should write
CObject *object = (CObject *) new AnyClassWithInheritedValuesFromCObject;

Second : to use non virtual fuction you could write

if(ID_TERRAIN == object->WhoAmI() )
{
CTerrain *terrain = (CTerrain*) object;
terrain->CallToNonVirtualFunction();
}
SkinnyM
SkinnyM
All right,

i belive i get what you are saying. So in my link node class, i could just do CObject *Instance; and as the start value, and then when it is passed a object to put in the list,

Instance = (CObject *) new ObjectPassIn;

then i could always just use the inherited functions that were given to ''ObjectPassIn'' from CObject right;

So say Update is a virtual function replaced by ObjectPassIn;

i could do

Instance->Update();
but i couldnt do,
Instance->GetWhatever(); because that is a function relative to ObjectPassIn not CObject?

But if i had to do that, i could do

CObjectPassIn ObjectInstance = (CObjectPassIn *)Object;
and then

ObjectInstance->GetWhatever(); would work.

If i understand you and the above is correct, then with virtual functions, i could use the base class but i could only call those virtual functions from the upper class. like update and such which were virtual.

Ok i think i get it.

if i did something wrong above, please tell me.

Thanks for the help
"What we do in life, echos in eternity" -- Gladiator

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.