Obj* fun() { Obj* obj = new Obj(123); obj->method(); if (*obj == Obj()) {delete obj; obj=NULL;} return obj; }
DynTmp<Obj> fun() { DynObj<Obj> obj( new Obj(123) ); obj().method(); if (*obj != Obj()) obj.reset(); return obj.moveToTmp(); }
Obj* fun(Obj* newObj) {
Obj* obj = newObj;
...
obj->method();
...
return obj;
}
RRef<Obj> fun(const RRef<Obj>& newObj) { RRef<Obj> obj( newObj ); ... obj().method(); ... return obj; }
class Foo { typedef std::list<Obj*> List; Foo() { list.push_back(new Bar(1)); list.push_back(new Bar(2)); (*list.begin())->method(); list.front()->method(); Bar* bar = new Bar(3); ... // hope no throw otherwise leak occurs! list.push_back(bar); } ~Foo() { /* loop over each item of list and call delete on it */ } List list; };
class Foo { typedef std::list<DynObj<Obj>::InValueContainer> List; Foo() { list.push_back( makeDynObj(new Bar(1)) ); list.push_back( makeDynObj(new Bar(2)) ); (**list.begin()).method(); (*list.front()).method(); DynObj<Bar> bar(new Bar(3)); ... // exception safe list.push_back(bar.giveAway()); } ~Foo() {} List list; };