Jean-François Brouillet wrote:
The following is a perfectly legal Java idiom which I find all over the place in the source I am porting.
Basically it says: For simple use, just use me as is. I implement a default policy regarding so and so. However you are free to specify your own policy when required:
interface I { void i() ; }
class C implements I {
I myI ;
void i() { System.out.println("I.i() called, as implemented by C") ; }
C() { myI = this ; }
void setI(I i) { myI = i ; }
void f() { myI.i() ; } }
You can do this with shared_ptr, but not with intrusive_ptr. ;-) class C: private I { shared_ptr<I> myI; void i(); // ... C( C const & ); C& operator=( C const & ); public: C(): myI( this, null_deleter() ) { } void setI( shared_ptr<I> const & i ) { myI = i; } }; A null_deleter works here because you know that the internal I and the shared_ptr member have the same lifetime, that of C. http://boost.org/libs/smart_ptr/sp_techniques.html#in_constructor "Depending on context, if the inner shared_ptr this_ doesn't need to keep the object alive, use a null_deleter as explained here and here." http://boost.org/libs/smart_ptr/sp_techniques.html#static