template <typename T>
class Shared_ptr {
public:
Shared_ptr(): count(new unsigned int(1)), val(new T()) {}
Shared_ptr(T* p): count(new unsigned int(1)), val(p) {}
Shared_ptr(const Shared_ptr& sp): count(sp.count), val(sp.val){
++*count;
}
~Shared_ptr() {
Release();
}
T& operator *() {
return *val;
}
Shared_ptr& operator = (const Shared_ptr& p) {
++ *p.count;
Release();
count = p.count;
val = p.val;
return *this;
}
void Release() {
if (--*count == 0) {
delete val;
delete count;
}
}
private:
unsigned int *count;
T *val;
};