//#pragma once
//#include<iostream>
//#include<string>
//using namespace std;
//template<class T>
//class autoPtr
//{
//public:
// autoPtr(T* ptr)
// :_ptr(ptr)
// {
// }
// autoPtr(const autoPtr<T>& ptr)
// :_ptr(new T(*(ptr._ptr)))
// {
// //operator=
//
// }
// autoPtr& operator=(autoPtr<T> ptr)
// {
// swap(_ptr,ptr._ptr);
// return *this;
// }
// void Display()
// {
// cout << (*_ptr) << endl;
// }
// ~autoPtr()
// {
// delete _ptr;
// }
// T& operator*()
// {
// assert(_ptr);
// return *_ptr;
// }
//
// T* operator->()
// {
// return _ptr;
// }
//
// T* GetPtr()
// {
// return _ptr;
// }
//protected:
// T* _ptr;
//};
//#pragma once
//#include<iostream>
//#include<string>
//template<class T>
//class ScopedPtr
//{
//public:
// ScopedPtr(T* ptr)
// :_ptr(ptr)
// {
//
// }
// ~ScopedPtr()
// {
// delete _ptr;
// }
// void Display()
// {
// cout << (*_ptr) << endl;
// }
// T& operator*()
// {
// //assert(_ptr);
// return *_ptr;
// }
//
// T* operator->()
// {
// return _ptr;
// }
//
// T* GetPtr()
// {
// return _ptr;
// }
//protected:
// T* _ptr;
//private:
// //不让调用
// ScopedPtr(const ScopedPtr<T>& s);
// ScopedPtr& operator=(const ScopedPtr<T> s);
//};
//#pragma once
//#include<iostream>
//#include<string>
//class Count;
//template<class T>
//class SharedPtr
//{
//public:
// SharedPtr()
// :_ptr(NULL)
// , _count(NULL)
// {}
// SharedPtr(T* ptr)
// :_ptr(ptr)
// , _count(new int(1))
// {}
// SharedPtr(SharedPtr<T>& s)
// :_ptr(s._ptr)
// , _count(s._count)
// {
// ++(*_count);
// }
// SharedPtr& operator=(SharedPtr<T>& s)
// {
// Destroy();
// _ptr = s._ptr;
// _count = s._count;
// ++s._count;
// return *this;
// }
// ~SharedPtr()
// {
// Destroy();
// }
// void Display()
// {
// cout << (*_ptr) << endl;
// }
// T& operator*()
// {
// //assert(_ptr);
// return *_ptr;
// }
//
// T* operator->()
// {
// return _ptr;
// }
//
// T* GetPtr()
// {
// return _ptr;
// }
//protected:
// void Destroy()
// {
// if (--(*_count) == 0)
// delete _ptr;
// }
//protected:
// T* _ptr;
// int* _count;
//};
//测试用例
//#include"autoPtr.hpp"
//#include<iostream>
//#include<string>
//using namespace std;
//void Test1()
//{
// int *p = new int(1);
// autoPtr<int> p1(p);
// autoPtr<int> p2(p1);
// cout << (*p) << endl;
// p1.Display();
// p2.Display();
//}
//void Test2()
//{
// int *p = new int(1);
// autoPtr<int> p1(p);
// int *p4= new int(2);
// autoPtr<int> p3 = p4;
// p3.Display();
// p3 = p1;
// p3.Display();
//}
//void Test3()
//{
// string *p = new string("abc");
// autoPtr<string> p1(p);
// p1.Display();
// autoPtr<string> p2(p1);
// p2.Display();
//}
//void Test4()
//{
// string *p = new string("abc");
// autoPtr<string> p1(p);
// p1.Display();
// string *pst = new string("def");
// autoPtr<string> p2(pst);
// p2.Display();
// autoPtr<string> p3(p1);
// p3.Display();
// p3 = p2;
// p3.Display();
//}
//int main()
//{
// Test4();
// system("pause");
// return 0;
//}
//#include"autoPtr.hpp"
//#include<iostream>
//using namespace std;
//#include<string>
////void Test1()
////{
//// int* p1 = new int(1);
//// ScopedPtr<int> p2(p1);
//// p2.Display();
//// cout << *p2 << endl;
////}
//
//void Test1()
//{
// int *p1 = new int(1);
// SharedPtr<int> p2(p1);
// SharedPtr<int> p3(p2);
// p2.Display();
// p3.Display();
//}
//void Test2()
//{
// int *p1 = new int(1);
// SharedPtr<int> p2(p1);
// SharedPtr<int> p3(p2);
// p3.Display();
// int *p4 = new int(2);
// SharedPtr<int> p5(p4);
// p3=p5;
// p3.Display();
//}
//void Test3()
//{
// string *p1 = new string("abc");
// SharedPtr<string> p2(p1);
// SharedPtr<string> p3(p2);
// p3.Display();
// string *p4 = new string("def");
// SharedPtr<string> p5(p4);
// p3 = p5;
// p3.Display();
//}
//int main()
//{
// Test3();
// system("pause");
// return 0;
//}本文出自 “小止” 博客,请务必保留此出处http://10541556.blog.51cto.com/10531556/1725200
原文:http://10541556.blog.51cto.com/10531556/1725200