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

STL vector

Started by PopJimmy Jun 1, 2003 at 12:11 AM 10 replies 1.3k views
Original Post
PopJimmy
PopJimmy
I am trying to use the STL vector in combination with multiple-inhearence: I must have a vector based on the class cObj, and i want to add a cBall obj that is derived from cObj. I thought of using pure virtual functions, but dunno how since i get compilation an error: "Can allocate an Object type 'cObj' since the function Draw() is abstract". #include <iostream> #include <vector> class cObj { public: virtual void Draw()=0; }; class cBall : public cObj { public: virtual void Draw() {cout << "o";} }; int main() { vector v; cBall *b = new cBall; v.push_back(*b); cObj *o = new cBall; v.push_back(*o); return 0; } Are stuff like dynamic/static casting the right sollution? (i dont know what casting are and why to use it anyway...) [edited by - PopJimmy on June 1, 2003 1:15:38 AM]
Zipster
Zipster
You can''t instantiate objects with pure virtual functions, so having CObj as the vector type implies to the compiler that it is instantiable You should use a pointer type instead, and thus store the pointers.

That is how polymorphism works anyway, with pointers.
PopJimmy
PopJimmy
So there is no way to use polymorphism with any contanier in the STL?
Zipster
Zipster
Yes, you store pointers. The containers do just that, contain, and if they contain pointers, then you can polymorph. (And that''s a cool word I just made up)
PopJimmy
PopJimmy
Can you give an example how the obj and ball classes should look like if wantde to use pointers? i´am a bit confused right now...
Neosmyle
Neosmyle

vector<cObj> v;
cBall *b = new cBall;
v.push_back(*b);
cObj *o = new cBall;
v.push_back(*o);
return 0;

would become

vector<cObj*> v;
cBall *b = new cBall;
v.push_back(b);
cObj *o = new cBall;
v.push_back(o);
return 0;
PopJimmy
PopJimmy
ok i get the point =)

thx guys
Expendable
Expendable
Ok, I''ve been struggling with a few of the finer points here. Maybe the problem is I need to relearn polymorphism in C++...

Using Neosmyle''s example...


vector v;
cBall *b = new cBall;
v.push_back(b);cObj *o = new cBall;
v.push_back(o);return 0;


After the b object has been put in the v vector, I know I can get at it again by something like this:

cObj *testobject = v[0];

That works fine. But I want to access the derived cBall class. The following does NOT seem to work:

cBall *testball = v[0]; // compile error!

cBall *testball = &testobject // compile error!

How can I access the derived cBall class from the cObj base class? I''ve been trying to read my "good" c++ book, and I''m not finding the answer!
E-x-p-e-n-d-a-b-l-e
SCRUB
SCRUB
casting them should work.

cBall *testball = (cBall*)v[0]; etc. etc.


LE SCRUB
Expendable
Expendable
Yes, that was the ticket! Thanks SCRUB!
E-x-p-e-n-d-a-b-l-e
Qw3r7yU10p!
Qw3r7yU10p!
quote:
Original post by Expendable
I want to access the derived cBall class



Why are you storing cObj* then? Using polymorphism means designing classes so that the base class, in this case cObj, has virtual functions which give the derived class the opportunity to do its own thing without you knowing its a different class . The specific function you have on cBall which doesn''t exist on cObj should be indirectly called by an overridden virtual function.

quote:
Original post by SCRUB
casting them should work.

cBall *testball = (cBall*)v[0]; etc. etc.



I felt physically sick when I read this.

For one thing if you''re certain that the vector will only store cBall*s then you might as well have a vector of cBall*s. If you''re not certain that it will only be cBall*s then the cast is evil. And finally the C-style cast that you''re using has been superceded by C++ casts. You would want to be using dynamic_cast.

However all of this is pointless as noted above. Casting is a last resort and can mean your design has gone badly wrong.
Jedyte
Jedyte
Indeed. Why do you use virtual functions when you use the concrete class? If you want to draw tour object just call
cObj* obj = v[0];
obj->Draw();



And the price we paid was the price men have always paid for achieving paradise in this life -- we went soft, we lost our edge.
- "Muad''Dib: Conversations" by the Princess Irulan
And the price we paid was the price men have always paid for achieving paradise in this life -- we went soft, we lost our edge. - "Muad'Dib: Conversations" by the Princess Irulan

Topic Locked

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

Sign in to reply to this topic.