Boost logo

Boost :

From: Ñåðãåé Èëüèí (spb.serg_at_[hidden])
Date: 2006-08-09 08:22:34


Let’s add this code into the weak_ptr:

shared_ptr<element_type> operator-> () const // throws bad_weak_ptr when expired

{

    shared_ptr<element_type> temp = lock();

    if( !temp )

        boost::throw_exception( boost::bad_weak_ptr() );

    return temp;

}

New way of use weak_ptr:

shared_ptr< MyClass > sp( new MyClass );

weak_ptr< MyClass > wp(sp);

try

{

      wp->Use1();

      wp->Use2();

}

catch( const exception& e )

{

      cout << e.what() << endl;

}

It seems to me, that my way is more convenient than old school:

 if(shared_ptr<MyClass> temp = wp.lock())

 {

      temp->Use1();

      temp->Use2();

 }

 else

      cout << "Cannot use, already destroyed" << endl ;

-- 
Sergey Ilyin (mailto:spb.serg_at_[hidden])

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk