Polymorphism and std::Vector

Y

Yuri Feldman

Hello all,

I would like to use a vector to store objects of different classes,
all (publicly) inherited from class Vehicle.

Is there a better way to do it than using a Vector<Vehicle*> i.e.
storing the pointers to these objects in the vector?

Is there an STL container better suited for this purpose? (I need to
be able to index it, like an array).

Thanks,
 
J

Juha Nieminen

Yuri said:
I would like to use a vector to store objects of different classes,
all (publicly) inherited from class Vehicle.

Is there a better way to do it than using a Vector<Vehicle*> i.e.
storing the pointers to these objects in the vector?

That is basically the only way of doing it. A std::vector can only
contain objects of the same type (and in this case this unique type is
"Vehicle*").

I assume that you know that you will have to be very careful with
memory management in this case, as C++ doesn't have (by default) garbage
collection. You could consider using smart pointers instead of raw
pointers (at least if the increased memory usage is a non-issue).
 
A

amrollahi.saeed

Hello all,

I would like to use a vector to store objects of different classes,
all (publicly) inherited from class Vehicle.

Is there a better way to do it than using a Vector<Vehicle*> i.e.
storing the pointers to these objects in the vector?

By better way what do you mean? If your class hierarchy is ready,
using std::vector<Vehicle *> is good approach. Indeed it is a typical
example
of combining two paradigms: generic programming and o-o programming.
BTW, STL doesn't have facilities like heterogeneous containers.
Is there an STL container better suited for this purpose? (I need to
be able to index it, like an array).

If you mean subscription operator, by index, vector like array has
index operator []. Indeed, vector has all major facilities of array
and more.

I hope it helps
- Saeed Amrollahi
 
L

Leandro Melo

  That is basically the only way of doing it. A std::vector can only
contain objects of the same type (and in this case this unique type is
"Vehicle*").

  I assume that you know that you will have to be very careful with
memory management in this case, as C++ doesn't have (by default) garbage
collection. You could consider using smart pointers instead of raw
pointers (at least if the increased memory usage is a non-issue).

And don't forget that std::auto_ptr is not the one you're looking for
(in this particular case, of course).
 
V

Vaclav Haisman

Yuri Feldman wrote, On 22.1.2009 11:10:
Hello all,

I would like to use a vector to store objects of different classes,
all (publicly) inherited from class Vehicle.

Is there a better way to do it than using a Vector<Vehicle*> i.e.
storing the pointers to these objects in the vector?
Nope, that's pretty much the only option. The only other option I can see is
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top