Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // C++03
- class Foo {
- std::vector<MyThing*> thingContainer_;
- public:
- ~Foo() { // have to manually delete everything
- std::vector<MyThing*>::iterator it = thingContainer_.begin();
- while(it != thingContainer.end()) {
- delete *it;
- it = thingContainer_.erase(it);
- }
- }
- MyThing* add_thing() {
- thingContainer_.push_back(new MyThing);
- return thingContainer_.back();
- }
- void remove_thing(MyThing* thing) {
- std::vector<MyThing*>::iterator it = thingContainer_.begin();
- for(; it != thingContainer_.end(); ++it) {
- if(*it == thing) {
- delete *it; // manually delete...
- thingContainer_.erase(it);
- return;
- }
- }
- }
- };
- // C++11
- class Foo {
- std::vector<std::shared_ptr<MyThing> > thingContainer_;
- public:
- ~Foo() {} // Don't need destructor any more! std::shared_ptr deletes all things automatically
- std::shared_ptr<MyThing> add_thing() {
- thingContainer_.push_back(new MyThing);
- return thingContainer_.back();
- }
- void remove_thing(MyThing* thing) {
- for(auto it = thingContainer_.begin(); it != thingContainer_.end(); ++it) {
- if(*it == thing) {
- thingContainer_.erase(it); // automatically also deletes the thing
- return;
- }
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement