智能指针是为了解决下面这个问题提出的
void func()
{
void *buf = (void*)malloc(1024);
if (some_fun())
throw exception();
//some thind ...
free(buf);
return;
}
如果上面代码异常发生了,free就可能不运行了.c++ 设计中就在 <memery> 中增加了智能指针.不用程序员显示释放内存了,是不是很炸天.
void func()
{
std::auto_ptr<int> ps(new int[1024]);
return;
}
我在网上找了一个自己实现的智能指针代码,方便理解
template <class T>
class my_unique_ptr
{
private:
T * ptr = nullptr;
public:
my_unique_ptr() : ptr(nullptr) // default constructor
{
}
my_unique_ptr(T * ptr) : ptr(ptr)
{
}
my_unique_ptr(const my_unique_ptr & obj) = delete; // copy constructor is deleted
my_unique_ptr& operator=(const my_unique_ptr & obj) = delete; // copy assignment is deleted
my_unique_ptr(my_unique_ptr && dyingObj) // move constructor
{
// Transfer ownership of the memory pointed by dyingObj to this object
this->ptr = dyingObj.ptr;
dyingObj.ptr = nullptr;
}
void operator=(my_unique_ptr && dyingObj) // move assignment
{
__cleanup__(); // cleanup any existing data
// Transfer ownership of the memory pointed by dyingObj to this object
this->ptr = dyingObj.ptr;
dyingObj.ptr = nullptr;
}
T* operator->() // deferencing arrow operator
{
return this->ptr;
}
T& operator*()
{
return *(this->ptr);
}
~my_unique_ptr() // destructor
{
__cleanup__();
}
private:
void __cleanup__()
{
if (ptr != nullptr)
delete ptr;
}
};
这个是share_ptr
template<class T>
class my_shared_ptr
{
private:
T * ptr = nullptr;
uint * refCount = nullptr;
public:
my_shared_ptr() : ptr(nullptr), refCount(new uint(0)) // default constructor
{
}
my_shared_ptr(T * ptr) : ptr(ptr), refCount(new uint(1)) // constructor
{
}
/*** Copy Semantics ***/
my_shared_ptr(const my_shared_ptr & obj) // copy constructor
{
this->ptr = obj.ptr; // share the underlying pointer
this->refCount = obj.refCount;
if (nullptr != obj.ptr)
{
(*this->refCount)++; // if the pointer is not null, increment the refCount
}
}
my_shared_ptr& operator=(const my_shared_ptr & obj) // copy assignment
{
__cleanup__(); // cleanup any existing data
// Assign incoming object's data to this object
this->ptr = obj.ptr; // share the underlying pointer
this->refCount = obj.refCount;
if (nullptr != obj.ptr)
{
(*this->refCount)++; // if the pointer is not null, increment the refCount
}
}
/*** Move Semantics ***/
my_shared_ptr(my_shared_ptr && dyingObj) // move constructor
{
this->ptr = dyingObj.ptr; // share the underlying pointer
this->refCount = dyingObj.refCount;
dyingObj.ptr = dyingObj.refCount = nullptr; // clean the dying object
}
my_shared_ptr& operator=(my_shared_ptr && dyingObj) // move assignment
{
__cleanup__(); // cleanup any existing data
this->ptr = dyingObj.ptr; // share the underlying pointer
this->refCount = dyingObj.refCount;
dyingObj.ptr = dyingObj.refCount = nullptr; // clean the dying object
}
uint get_count() const
{
return *refCount; // *this->refCount
}
T* get() const
{
return this->ptr;
}
T* operator->() const
{
return this->ptr;
}
T& operator*() const
{
return this->ptr;
}
~my_shared_ptr() // destructor
{
__cleanup__();
}
private:
void __cleanup__()
{
(*refCount)--;
if (*refCount == 0)
{
if (nullptr != ptr)
delete ptr;
delete refCount;
}
}
};