🔒 Locked
A vector list of VOID* objects?
Advertisement
Original Post
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.?
You can safely downcast with dynamic_cast:
But often when you need downcasting, there's something fishy in your code.
[edited by - civguy on February 22, 2003 10:23:04 AM]
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]
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."
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."
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.
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.
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.
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.
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.
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.
Advertisement