模拟实现auto_ptr、scoped_ptr、shared_ptr等智能指针

智能指针:所谓智能指针就是智能/自动化的管理指针所指向的动态资源的释放。

auto_ptr 有缺陷,自己赋值给自己,会失效置空

scoped_ptr 解决了auto_ptr缺陷,防拷贝

shared_ptr 含有引用计数  weak_ptr打破循环引用,不增加引用计数

#include<iostream>
using namespace std;

//模拟auto_ptr
template<class T>
class SmartPtr
{
public:
	SmartPtr(T* ptr = NULL)
		:_ptr(ptr)
	{}
	SmartPtr(SmartPtr<T>& sp)
	{
		_ptr = sp._ptr;
		sp._ptr = NULL;
	}
	SmartPtr<T>& operator=(SmartPtr<T>& sp)
	{
		if (this != &sp)
		{
			_ptr = sp._ptr;
			sp._ptr = NULL;
		}
		return *this;
	}
	~SmartPtr()
	{
		if (_ptr)
		{
			delete _ptr;
		}
	}
	T* operator->()
	{
		return _ptr;
	}
	T& operator*()
	{
		return *_ptr;
	}
private:
	T* _ptr;
};

class AA
{
public:
	  AA()
	  {
		  cout<<"AA()"<<endl;
	  }
	
	  ~AA()
      {
		  cout<<"~AA()"<<endl;
	  }
	  void Print()
	 {
		  cout<<"Print()"<<endl;
	 }
public:
		int _aa;
};
void Test1()
{
	SmartPtr<AA> p1(new AA);
	SmartPtr<AA> p2(p1);
	SmartPtr<AA> p3;
	p3 = p2;
	p1->Print();
	(*p3).Print();
}
int main()
{
	Test1();
	system("pause");
	return 0;
}



#include<iostream>
#include<string>
using namespace std;

//模拟scoped_ptr
template<class T>
class SmartPtr
{
public:
	SmartPtr(T* ptr = NULL)
		:_ptr(ptr)
	{}

	~SmartPtr()
	{
		if (_ptr)
		{
			delete _ptr;
		}
	}
	T* operator->()
	{
		return _ptr;
	}
	T& operator*()
	{
		return *_ptr;
	}
private:
	SmartPtr(SmartPtr<T>& sp)
	{
		_ptr = sp._ptr;
		sp._ptr = NULL;
	}
	SmartPtr<T>& operator=(SmartPtr<T>& sp)
	{
		if (this != &sp)
		{
			_ptr = sp._ptr;
			sp._ptr = NULL;
		}
		return *this;
	}
private:
	T* _ptr;
};

class AA
{
public:
	AA()
	{
		cout << "AA()" << endl;
	}

	~AA()
	{
		cout << "~AA()" << endl;
	}
	void Print()
	{
		cout << "Print()" << endl;
	}
public:
	int _aa;
};
void Test2()
{
	SmartPtr<AA> p1(new AA);
	SmartPtr<AA> p3;
	p1->Print();
	(*p3).Print();
}
int main()
{
	Test2();
	system("pause");
	return 0;
}




#include<iostream>
using namespace std;

//模拟shared_ptr
template<class T>
class SmartPtr
{
public:
	SmartPtr(T* ptr = NULL)
		:_ptr(ptr)
		, _CountPtr(new int(1))
	{
		(*_CountPtr)++;
	}
	SmartPtr(SmartPtr<T>& sp)
	{
		_ptr = sp._ptr;
		_CountPtr = sp._CountPtr;
		(*_CountPtr)++;
	}
	SmartPtr<T>& operator=(SmartPtr<T>& sp)
	{
		if (this != &sp)
		{
			if (--(*_CountPtr) && _ptr)
			{
				delete _ptr;
				delete _CountPtr;
			}
			_ptr = sp._ptr;
			_CountPtr = sp._CountPtr;
			(*_CountPtr)++;
		}
		return *this;
	}
	~SmartPtr()
	{
		if (--(*_CountPtr)&&_ptr)
		{
			delete _ptr;
			delete _CountPtr;
		}
	}
	int GetRefCount()
	{
		return *_CountPtr;
	}
	T* operator->()
	{
		return _ptr;
	}
	T& operator*()
	{
		return *_ptr;
	}
private:
	T* _ptr;
	int* _CountPtr;//指向引用计数的指针
};

class AA
{
public:
	AA()
	{
		cout << "AA()" << endl;
	}

	~AA()
	{
		cout << "~AA()" << endl;
	}
	void Print()
	{
		cout << "Print()" << endl;
		
	}
public:
	int _aa;
};
void Test3()
{
	SmartPtr<AA> p1(new AA);
	SmartPtr<AA> p2(p1);
	SmartPtr<AA> p3;
	p3 = p2;
	p1->Print();
	(*p3).Print();
}
int main()
{
	Test3();
	system("pause");
	return 0;
}


 

你可能感兴趣的:(模拟实现auto_ptr、scoped_ptr、shared_ptr等智能指针)