For Programmers: Free Programming Magazines  


Home > Archive > VC STL > August 2005 > Quick STL Vector Question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Quick STL Vector Question
bob

2005-08-16, 4:02 am

Hi

I have learnt that to store objects in an STL Vector, that the object
class must have default constructor, == operator and < operator
defined.

Does the same precondition apply when storing _pointers_ to objects in
a vector?

Thanks,
Bob

Bo Persson

2005-08-16, 4:02 am


"bob" <bobtabulous@yahoo.co.uk> skrev i meddelandet
news:1124178454.678646.26700@g44g2000cwa.googlegroups.com...
> Hi
>
> I have learnt that to store objects in an STL Vector, that the
> object
> class must have default constructor, == operator and < operator
> defined.


Not entirely true. To store an object in a vector, the object must be
of a type that is assignable and copy constructible. The default
constructor is only required if you use certain functions, like
resize().

Comparisons are not required by vector. They are required by ordered
containers, like map and set.

>
> Does the same precondition apply when storing _pointers_ to objects
> in
> a vector?


In that case the requirements apply to the pointer only.



Bo Persson



bob

2005-08-16, 5:10 pm

Could you elaborate on the second part please? Do my objects have to be
assignable / copy constructable, even if I am storing pointers in the
vector?

Thanks,
Robert

Bo Persson

2005-08-16, 5:10 pm


"bob" <bobtabulous@yahoo.co.uk> skrev i meddelandet
news:1124183238.970084.201100@z14g2000cwz.googlegroups.com...
> Could you elaborate on the second part please? Do my objects have to
> be
> assignable / copy constructable, even if I am storing pointers in
> the
> vector?


No, if you store pointers in the vector, it is the pointers that have
to be assignable and copy constructible. And they are.

The vector doesn't care about the objects pointed to. These obejcts
are your responsibility.


Bo Persson



>
> Thanks,
> Robert
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com