#include #include #include #include #include #include using namespace std; using boost::scoped_ptr; using boost::shared_ptr; using boost::weak_ptr; //--------------------------------------------------------------------------- class A { public: A(const char *pdestroy_message); ~A(); const char* const getMessage() const; private: char* pmsg; }; //--------------------------------------------------------------------------- class B { public: B(); ~B(); }; //--------------------------------------------------------------------------- A::A(const char* pdestroy_message) : pmsg(0) { pmsg = new char[strlen(pdestroy_message) + 1]; strcpy(pmsg, pdestroy_message); cout << "A is created!" << " " << pmsg << endl; } //--------------------------------------------------------------------------- A::~A() { cout << pmsg << endl; delete[] pmsg; } //--------------------------------------------------------------------------- const char* const A::getMessage() const { return pmsg; } //--------------------------------------------------------------------------- B::B() { cout << "B is being created!" << endl; throw std::logic_error("Too bad!"); } //--------------------------------------------------------------------------- B::~B() { cout << "B is destroyed!" << endl; } //--------------------------------------------------------------------------- shared_ptr spglobal(new A("Global shared_ptr destroyed!")); //--------------------------------------------------------------------------- void main() { weak_ptr wpglobal(spglobal); shared_ptr splocal = wpglobal.lock(); if ( splocal ) try { A a("Local A variable destroyed!"); auto_ptr apa(new A("auto_ptr destroyed!")); scoped_ptr scpa(new A("scoped_ptr destroyed!")); shared_ptr spa(new A("Local shared_ptr destroyed!")); cout << "Access to global object through the local one!" << " " << splocal->getMessage() << endl; B b; } catch( std::exception& e) { cout << "Exception cought: " << " " << e.what() << endl; } }