class Obj_A {
public:
~Ojb_A() {
cout << "calling Obj_A() destructor\n";
}
void method1() {
cout << "invoking Obj_A::method1()\n";
}
};
class Obj_B {
boost::shared_ptr<Obj_A> _objA;
public:
Obj_B(Obj_A *objA) {
_objA.reset(objA)
}
void method1() { _objA->method1(); }
};
class ObjAFactory {
public
static Obj_A* createObjA();
};
Obj_A* ObjAFactory::createObjA() {
boost::shared_ptr<Obj_A> objA(new Obj_A());
return objA.get();
}
void main() {
boost::shared_ptr<Obj_A> objA(ObjAFactory::createObjA());
Obj_B objB(objA);
objB.method1();
}
Output:
*calling Obj_A() destructor
invoking Obj_A::method1()
calling Obj_A() destructor
calling Obj_A() destructor
a.out in free(): warning: page is already free
a.out in free(): warning: page is already free*