So, to protect this problem, you can make your counterstructures private and provide only creation functions that return shared_ptr - this way, the object cannot be allocated on the stack, for example:
class C : public enable_shared_from_this<C> { public: static shared_ptr<C> create() { return shared_ptr<C>(new C() ); } shared_ptr<C> method() { shared_from_this(); } private: C() {...}
If you want an anonymizer = you can provide a clone function using a private implemented copy constructor, something like this
// This goes in class C shared_ptr<C> C::clone() const { return shared_ptr<C>( new C(*this) ); } // This is how you can use it shared_ptr<C> c2 = c1->clone();
Michael anderson
source share