• Advertisement
Sign in to follow this  

dangerous vector use due to scope?

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

class A { std::vector<std::pair<std::string, HWND> > v; void method() { std::pair<std::string, HWND> p("hello",0x0); v.push_back(p); } }; is this going to be bad news, because v is storing a reference to a p with local scope? is my only alternative to allocate a persistent p using new and remember to clean up later?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by thedustbustr
class A {
std::vector<std::pair<std::string, HWND> > v;

void method() {
std::pair<std::string, HWND> p("hello",0x0);
v.push_back(p);
}
};

is this going to be bad news, because v is storing a reference to a p with local scope? is my only alternative to allocate a persistent p using new and remember to clean up later?


No, because vector doesn't store references, it stores copies. It will call the copy constructor of the std::pair and store a copy.

Share this post


Link to post
Share on other sites
Because you're using containers, you do not need to worry about such things.

Share this post


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

  • Advertisement