let's have std::list of class t.
what best way manage these elements? considering manager(i mean - 1 owner) can add or remove items list.
1)
std::list < t* > mylist; //adding new element mylist.push_back( new t(..) ); //deleting 1 element ...roaming through list...got it... delete *iterator; mylist.erase(iterator); 2)
std::list < std::unique_ptr<t> > mylist; //adding new element mylist.push_back ( std::unique_ptr<t>( new t(..) ); //deleting 1 element ...roaming through list...got it... mylist.erase(iterator);
if ownership model in program list "owns" elements inside it, second way (i.e. unique_ptr<t>) better. lets c++ manage resources of list automatically, important in situations when list declared in local scope, because not have worry exiting scope prematurely.
Comments
Post a Comment