Jump to content
  • Advertisement
Sign in to follow this  
MENTAL

(C++) std::vector, std::string and structures (Solved)

This topic is 4932 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If I create a container of structures which have std::strings in them, will the std::strings be deep copied or shallow copied? Or do I have to convert it into a class and provide a deep copy constructor? Edit: (Solved) [Edited by - MENTAL on January 17, 2005 2:50:01 PM]

Share this post


Link to post
Share on other sites
Advertisement
Unless they are stored by pointer or reference, they are copied just like any built in type.

Share this post


Link to post
Share on other sites
This code is standards compliant:


struct Emp
{
std::string firstName;
std::string lastName;
std::string position;
};

std::list<Emp> employeeList;

Emp e;
e.firstName = "John";
e.lastName = "Doe";
e.position = "Cashier";

employeeList.push_back(e);

e.firstName = "Bob";
e.lastName = "Smith";
e.position = "Bag boy";

employeeList.push_back(e);



The reason for that is that if you do not provide a copy constructor, then c++ will create a default copy ctor which consists of calling the copy ctor of all the members.

Share this post


Link to post
Share on other sites
in general when using the STL, everything is deep copied unless you specifically pass the STL structures by reference or by pointer. so functions like this are generally bad:


void doSomething( std::vector< MyClass > allMyClasses )
{
}





the vector used inside of doSomething will be a complete copy of the vector that was passed in. in your case you'd be deep copying everything: the vector and all the strings it contains.

-me

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!