
9 Aug
2006
9 Aug
'06
12:22 p.m.
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@mail.ru)