Design smart pointer class
Utilisateur anonyme
template struct sp { sp(const T& p):p(p) { ref=new int(); *ref=1; } sp(const sp& other) { ref=other.ref; p=other.p; ++*ref; } ~sp() { if (ref) { if (--*ref==0) { delete ref; delete p; } } T& operator *(int) { return *p; } const T& operator *() const { return *p; } T* operator ->(int) { return p; } const T* operator ->() const { return p; } int *ref; T* p; };