#pragma once
template<typename T> class nc_ptr
{
int* m_ref;
T* m_t;
public:
nc_ptr()
{
m_t = new T;
m_ref = new int;
*m_ref = 1;
};
nc_ptr(const nc_ptr<T>& rhs)
{
m_ref = rhs.m_ref;
++(*m_ref);
m_t = rhs.m_t;
}
~ nc_ptr()
{
if(--(*m_ref) == 0)
{
delete m_ref;
delete m_t;
}
};
nc_ptr<T>& operator= (const nc_ptr<T>& rhs)
{
++(*rhs.m_ref);
if(--(*m_ref) == 0)
{
delete m_ref;
delete m_t;
}
m_ref = rhs.m_ref;
m_t = rhs.m_t;
};
T* operator-> () const
{
return m_t;
};
};
迷你智能指针
最新推荐文章于 2023-06-08 15:22:49 发布