
13 Apr
2007
13 Apr
'07
7:26 a.m.
Peter Dimov a écrit :
If you are using a Boost release, you can do:
shared_ptr<Owned> getOwned( shared_ptr<Owner> p, int i ) { return shared_ptr<Owned>( &p->owned[ i ], boost::bind( &shared_ptr<Owner>::reset, p ) ); }
which is essentially your:
- Make unknownType a class that contain a shared_ptr<Owner> and a Owned*, and forward all its functions to Owned.
Of course you need to keep the vector<> from reallocating since your pointer will dangle.
If you are using the very latest CVS version that includes explicit support for shared_ptr aliasing, it's even easier:
shared_ptr<Owned> getOwned( shared_ptr<Owner> p, int i ) { return shared_ptr<Owned>( p, &p->owned[ i ] ); }
Thank you ! -- Loïc