C++ Question about generic object containers
Say I have the following:
struct Object
{
// stuff
}
struct Ball : public Object
{
int ID;
int pos_x;
int pos_y;
int vel_x;
int vel_y;
}
struct Enemy : public Object
{
int ID;
int flags;
int type;
int subtype;
int data;
int pos_x;
int pos_y;
}
So, assuming the above is legal, I then do this:
Object* pobject[4];
pobject[0] = new Ball;
pobject[1] = new Ball;
pobject[2] = new Enemy;
pobject[3] = new Enemy;
Ok, assuming the above is legal, can I then do this:
pobject[0]->ID = GetID();
pobject[0]->pos_x = 100;
pobject[0]->pos_y = 150;
pobject[0]->vel_x = 10;
pobject[0]->vel_x = -8;
pobject[1]->ID = GetID();
pobject[1]->pos_x = 80;
pobject[1]->pos_y = 40;
pobject[1]->vel_x = -3;
pobject[1]->vel_x = 10;
pobject[2]->ID = GetID();
pobject[2]->flags = 0x0001;
pobject[2]->type = 4;
pobject[2]->subtype = 2;
pobject[2]->data = 4;
pobject[2]->pos_x = 200;
pobject[2]->pos_y = 300;
pobject[3]->ID = GetID();
pobject[3]->flags = 0x0001;
pobject[3]->type = 4;
pobject[3]->subtype = 1;
pobject[3]->data = 3;
pobject[3]->pos_x = 300;
pobject[3]->pos_y = 200;
Is that legal?
I guess what I''m asking is, will C++ automagically "know" what type of object is being pointed to? My guess is no, but how else would I implement a generic object container, if I or C++ doesn''t know what each (Object*) points to?
No, it''s not legal.
If you have an Object pointer, the only thing you can do is call members of the Object class.
You may cast an Object down to a subclass, but unless you _know_ that the underlying Object is really what you''re trying to cast into, this can be dangerous.
Look at it this way: your Object class is an interface to all subclasses. Everything you want to be able to do to a subclass needs to be accessible through the Object class. This is why you have virtual functions--each subclass of Object can override a function to have it do what you want.
If you have an Object pointer, the only thing you can do is call members of the Object class.
You may cast an Object down to a subclass, but unless you _know_ that the underlying Object is really what you''re trying to cast into, this can be dangerous.
Look at it this way: your Object class is an interface to all subclasses. Everything you want to be able to do to a subclass needs to be accessible through the Object class. This is why you have virtual functions--each subclass of Object can override a function to have it do what you want.
Ok, I think I geddit.
Thanks for the clarification. I think I know where to go with my project now.
Thanks for the clarification. I think I know where to go with my project now.
quote:
Original post by Stoffel
You may cast an Object down to a subclass, but unless you _know_ that the underlying Object is really what you''re trying to cast into, this can be dangerous.
You could use RTTI to solve this, by using dynamic_cast. If the downcast fails (ie the type that is being cast to is not a subclass of Object), it simply returns a null pointer.
However, the question is if that''s a good solution, because using RTTI will generate some runtime overhead. A better solution may be to use virtual method calls.
HTH
Some useful C++ links:Free multiplatform ANSI C++ Standard Library implementationVisual C++ 6.0 STL fixesVisual C++ 6.0 noncompliance issuesC++ FAQ Lite
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement