基于引用计数的智能指针简单实现
#include <iostream>
using namespace std;
template<class T>
class SmartPtr
{
public:
SmartPtr(T *p);
~SmartPtr();
SmartPtr(const SmartPtr<T> &orig); // 浅拷贝
SmartPtr<T>& operator=(const SmartPtr<T> &rhs); // 浅拷贝
private:
T *ptr;
// 将use_count声明成指针是为了方便对其的递增或递减操作
int *use_count;
};
template<class T>
SmartPtr<T>::SmartPtr(T *p) : ptr(p)
{
try
{
use_count = new int(1);
}
catch (...)
{
delete ptr;
ptr = nullptr;
use_count = nullptr;
cout << "Allocate memory for use_count fails." << endl;
exit(1);
}
cout << "Constructor is called!" << endl;
}
template<class T>
SmartPtr<T>::~SmartPtr()
{
// 只在最后一个对象引用ptr时才释放内存
if (--(*use_count) == 0)
{
delete ptr;
delete use_count;
ptr = nullptr;
use_count = nullptr;
cout << "Destructor is called!" << endl;
}
}
template<class T>
SmartPtr<T>::SmartPtr(const SmartPtr<T> &orig)
{
ptr = orig.ptr;
use_count = orig.use_count;
++(*use_count);
cout << "Copy constructor is called!" << endl;
}
// 重载等号函数不同于复制构造函数,即等号左边的对象可能已经指向某块内存。
// 这样,我们就得先判断左边对象指向的内存已经被引用的次数。如果次数为1,
// 表明我们可以释放这块内存;反之则不释放,由其他对象来释放。
template<class T>
SmartPtr<T>& SmartPtr<T>::operator=(const SmartPtr<T> &rhs)
{
// 《C++ primer》:“这个赋值操作符在减少左操作数的使用计数之前使rhs的使用计数加1,
// 从而防止自身赋值”而导致的提早释放内存
++(*rhs.use_count);
// 将左操作数对象的使用计数减1,若该对象的使用计数减至0,则删除该对象
if (--(*use_count) == 0)
{
delete ptr;
delete use_count;
cout << "Left side object is deleted!" << endl;
}
ptr = rhs.ptr;
use_count = rhs.use_count;
cout << "Assignment operator overloaded is called!" << endl;
return *this;
}
测试程序
#include <iostream>
#include "smartptr.h"
using namespace std;
int main()
{
// Test Constructor and Assignment Operator Overloaded
SmartPtr<int> p1(new int(0));
p1 = p1;
// Test Copy Constructor
SmartPtr<int> p2(p1);
// Test Assignment Operator Overloaded
SmartPtr<int> p3(new int(1));
p3 = p1;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
template<typename T>
class SmartPtr{
private:
T *ptr;
int *use_count;
public:
SmartPtr(T *p); //SmartPtr<int> p(new int(2))
SmartPtr(const SmartPtr<T> &rhs); //q(p)
SmartPtr<T> &operator=(const SmartPtr<T> &rhs);//q=p
~SmartPtr();
T operator*(); //解引用操作
T* operator->();//取成员操作
T* operator+(int i); //指针加一个常数操作
int operator-(SmartPtr<T> &t2); //定义两个指针相减
int getcount(){
return *use_count;
}
};
template<typename T>
int SmartPtr<T>::operator-(SmartPtr<T> &t2) {
return ptr-t2.ptr;
}
template<typename T>
SmartPtr<T>::SmartPtr(T *p) {
ptr = p;
try{
use_count = new int(1);
}catch(...)
{
delete ptr;
ptr = nullptr;
delete use_count;
use_count = nullptr;
}
}
template<typename T>
SmartPtr<T>::SmartPtr(const SmartPtr<T> &rhs){
use_count = rhs.use_count;
this->ptr = rhs.ptr;
++(*use_count);
}
template<typename T>
SmartPtr<T>& SmartPtr<T>::operator=(const SmartPtr<T> &rhs) {
++*(rhs.use_count);
if((--*(use_count))==0){
delete ptr;
ptr= nullptr;
delete use_count;
use_count = nullptr;
}
ptr = rhs.ptr;
use_count = rhs.use_count;
return *this;
}
template<typename T>
SmartPtr<T>::~SmartPtr() {
if(--(*use_count)==0){
delete ptr;
ptr= nullptr;
delete use_count;
use_count = nullptr;
}
}
template<typename T>
T SmartPtr<T>::operator*() {
return *ptr;
}
template<typename T>
T* SmartPtr<T>::operator->(){
return ptr;
}
template<typename T>
T* SmartPtr<T>::operator+(int i){
T *temp = ptr+i;
return temp;
}
int main(){
SmartPtr<int> sp = new int(12);
SmartPtr<int> q=sp;
cout<<sp.getcount()<<endl;
}