I have a c++ class Foo with an stl std::string
as member. Further I have a STL std::vector<Foo> vecFoo
containing objects of class Foo by value (not pointers to objects of Foo
). Now I was advised by a friend not to implement it in this way but to build the vector with pointers: std::vector<Foo*>
or use boost smart pointers. He discussed that a vector, which involves lots of copy operations on its members (allocating more space when adding members etc.) will make problems when the contained classes have dynamic members like std::string
. Might there occur any problems?
For my understanding the std::string
actually does a deep copy (or copy on write) when class Foo might be copied by the std::vector
, as Foo calls copy constructors for all of its members. My friend argued that it is a problem if the string members are of different length among all Foo objects in the vector when the vector is allocating new space. What do you think?
The only reason for using pointers to Foo
inside the vector is speed. A pointer to Foo (Foo*
) is copied much faster than the complete class Foo
, isnt it?
Thx for discussion!