Skip to main content
gamedev.net
🔒 Locked

A vector list of VOID* objects?

Started by GekkoCube Feb 22, 2003 at 9:08 AM 7 replies 200+ views
Original Post
GekkoCube
GekkoCube
The problem is this: I currently have declared a vector list of the base class Object. In my render function I simply go thru this vector list and call the Render function, which i declared to be a virtual function. There is an object that is derived from this base class but has a unique function that is not defined as virtual in the base class. So basically, I can''t really call that function since my list is of type Object, and therefore the function call unreferenced. I tried making a virtual function in the Object class that simply says: CObject GetObject() { return 0; } ...which would basically return a this pointer from their respective derived object. But this is a problem since the list is still of type Object. I even tried defining the vector list as of type VOID*. Am I on the right track of doing this...because even the VOID* doesnt seem to allow me to access the methods that I need.?
civguy
civguy
You can safely downcast with dynamic_cast:

      
Object* someObject = ...;

Derived* someDerived = dynamic_cast<Derived*>(someObject);
if (someDerived) {
//do something with derived, like call it's special functions

} else {
//argh, someObject couldn't be casted to Derived-type

}

But often when you need downcasting, there's something fishy in your code.


[edited by - civguy on February 22, 2003 10:23:04 AM]
George2
George2
dynamic_cast is what you need(actually a redesign of your code) :


  

Object* obj = new WhatEver();
WhatEver* a; // WhatEver is derived from Object

a = dynamic_cast<WhatEver*>(obj);
// note if obj isn''t a pointer to a WhatEver thing dynamic_cast will return NULL


"THE INFORMATION CONTAINED IN THIS REPORT IS CLASSIFIED; DO NOT GO TO FOX NEWS TO READ OR OBTAIN A COPY." , the pentagon
GekkoCube
GekkoCube
thanks a bunch.
but i am convinced now that their is something fishy with my code and that it needs redesigning (according to the top 2 replies).

The question now is; what design can accomplish what im trying to do without down casting???
civguy
civguy
We know very little of your problem and there''s no generic answer.

What is the unique derived-class function you want to call and where do you want to call it?
GekkoCube
GekkoCube
Sorry about that. I thought it was clear on my original post, so here it is...

The specific derived class function im trying to call is

void SetCameraPos( camera );

I want to call this every frame.

Now my base class has virtual/pure virtual functions...

virtual ~Object();
virtual Create();
virtual Render() = 0;

So, when i try to do this...

objects[index]->SetCameraPos( camera );

...I receive an error because of the list of base class object.
i could down cast it, and it would probably work fine...but i was just simply wondering if there was another way since i was told that my style was "fishy."
civguy
civguy
SetCameraPos() should be a virtual method in Object-class if it''s applicable to each of Object''s subclasses. It could be in Object-class even if there are some (very few) classes for which it isn''t applicable, and just have empty implementation for those. But if only a few Objects need SetCameraPos(), you could make another container for those (one that stores pointers to Derived-class), possibly have a new class that manages such objects.

But surely there''s no ultimate answer (I still think the info you''ve given is quite vague) and dynamic_cast isn''t inherently evil either. Use it if it leads to cleanest looking code.
GekkoCube
GekkoCube
Sorry about vagueness.
I will clear it all up here, once and for all, although I will adopt one of your suggestions (probably including the SetCameraPos() in Object class).

my list of derived objects are...

Skybox and Terrain, nothing else for now.

Currently, only Terrain needs the SetCameraPos().
Plus, my Object class seems like it shouldnt have a SetCameraPos, although it probably will now.
GekkoCube
GekkoCube
Wait! The reason why i have a list of base class pointers is because of relative simplicity and readability.
How can i make another list of derived class pointers???

Right now my list is declared as follows:
CObject *list = new CObject[size];

... or something close to it, so every element in this list is of type CObject, which is why im deriving everything from.

Topic Locked

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

Sign in to reply to this topic.