Sign in to follow this  
thedustbustr

dangerous vector use due to scope?

Recommended Posts

thedustbustr    191
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
Omid Ghavami    1007
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this