Jump to content
  • Advertisement
Sign in to follow this  
xegoth

Ugh, haven't programmed in C++ in forever, simple vector question.

This topic is 4493 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

I've been coding in C# for the last year or so and recently returned to C++ only to find I seem to have forgotten everything. Heh. Anyhow... given the following scenario...
class foo
{
list<std::string> m_list;

foo(int Count)
{
     for(int i = 0; i < NumSeats; i++)
     {
         string bar = "something";
	 m_list.push_back(bar);
     }
}

};
Okay. What happens to the data in the list? I want to think that the string is in the scope of the for loop, so it goes out of scope each time around, meaning the list is empty. What *really* happens?

Share this post


Link to post
Share on other sites
Advertisement
When you insert something into a standard library container, the object is not put into it. Instead a copy of the object is made. Which means that when the string you are inserting into the list goes out of scope you don't have to worry about the string actually in the list, as it is a different object and has not gone out of scope.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
m_list is a list of strings.

every time the loop runs, you COPY the string "something" into the last position in the list.

So after the loop has finished, there will be 'NumSeats' string objects, all containing the text "something".


if you wrote it like this:
class foo
{
list m_list;

foo(int Count)
{
for(int i = 0; i <

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
arg my post got killed coz it treated the > and < as HTML...

Share this post


Link to post
Share on other sites
Quote:
Original post by Will F
When you insert something into a standard library container, the object is not put into it. Instead a copy of the object is made.


Ah, okay. I didn't realize STL makes a copy, the fact that it was passed by reference is what sent me down that path. Thanks.

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!