
Michael Anderson wrote:
<snip>
For example I want this to work... where ptr<Bar> is some kind of smart (or otherwise) pointer to a bar object.
{ ptr<Bar> barPtr; { ptr<Foo> fooPtr(new Foo); barPtr = fooPtr->bar; //A } barPtr->doSomething(); //B //C }
Note that fooPtr goes out of scope at line A, but if the Foo object is destroyed at A then line B is an error. Instead I want the Foo object to exist until line C where both barPtr and fooPtr go out of scope.
<snip>
I see... This sort of usage smells bad to me but if you have solid reasons behind it perhaps this is a case for 2 shared pointers using the same reference count object as was discussed on this list a while ago. i.e. something like class Bar {}; class Foo : enable_shared_from_this<Foo> { public: Foo() : bar( shared_ptr<Bar>(new Bar, shared_from_this()) ) {} weak_ptr<Bar> bar; }; { shared_ptr<Bar> barPtr; { shared_ptr<Foo> fooPtr(new Foo); barPtr = fooPtr->bar.lock(); } //A barPtr->doSomething(); } //B where nothing gets destroyed at //A but both get destroyed at //B hmm I don't think this will quite work but I'll post it anyways to get more ideas. - Michael Marcin