vector out of scope... how to free memory?

Hello.

Suppose I have a class Widget and I do something like the following:

vector<Widget*> wlist;

wlist.push_back( new Widget());
wlist.push_back( new Widget());
...

Now, when the wlist goes out of scope, is there a nice way for the allocated Widget to be free'd? Is there a C++ idiom to do this? I thought auto_ptr would, but then I read you shouldn't use it in a STL.

thanks.
Yes, C++11's unique_ptr. Prior to C++11, one would generally use a ptr_vector implementation to solve the same problem.
1
2
3
4
std::vector< std::unique_ptr<Widget> > wlist ;

wlist.emplace_back( new Widget() ) ;
wlist.emplace_back( new Widget() ) ;


http://www.devx.com/cplus/10MinuteSolution/39071/1954
Topic archived. No new replies allowed.