如何养成良好的C++编程习惯(一)―― 内存管理 (六)

2014-11-24 12:08:21 · 作者: · 浏览: 4
<_Ty>& operator = (_Ty* _Ptr)
{return (smart_gd_simple_ptr<_Ty>&)__super::operator = (_Ptr);}

private:
template smart_gd_simple_ptr<_Ty> (const smart_ptr<_Ty, _Other>&);
template smart_gd_simple_ptr<_Ty>& operator = (const smart_ptr<_Ty, _Other>&);

template smart_gd_simple_ptr<_Ty> (const smart_gd_simple_ptr<_Other>&);
template smart_gd_simple_ptr<_Ty>& operator = (const smart_gd_simple_ptr<_Other>&);
};

/************************************************************************/
/* smart_array_ptr 数组智能指针 */
/************************************************************************/

template
class smart_array_ptr : public smart_ptr<_Ty, array_deleter<_Ty>>
{
public:
smart_array_ptr(_Ty* _Ptr = 0) : smart_ptr(_Ptr) {}
smart_array_ptr(smart_simple_ptr<_Ty>& _Right) : smart_ptr(_Right) {}
smart_array_ptr(smart_ptr<_Ty, array_deleter<_Ty>>& _Right) : smart_ptr(_Right) {}

smart_array_ptr<_Ty>& operator = (smart_ptr<_Ty, array_deleter<_Ty>>& _Right)
{return (smart_array_ptr<_Ty>&)__super::operator = (_Right);}

smart_array_ptr<_Ty>& operator = (smart_array_ptr<_Ty>& _Right)
{return (smart_array_ptr<_Ty>&)__super::operator = (_Right);}

smart_array_ptr<_Ty>& operator = (_Ty* _Ptr)
{return (smart_array_ptr<_Ty>&)__super::operator = (_Ptr);}

private:
template smart_array_ptr<_Ty> (const smart_ptr<_Ty, _Other>&);
template smart_array_ptr<_Ty>& operator = (const smart_ptr<_Ty, _Other>&);

template smart_array_ptr<_Ty> (const smart_array_ptr<_Other>&);
template smart_array_ptr<_Ty>& operator = (const smart_array_ptr<_Other>&);
};

/************************************************************************/
/* smart_gd_array_ptr 数组智能指针 (使用全局 delete) */
/************************************************************************/

template
class smart_gd_array_ptr : public smart_ptr<_Ty, global_array_deleter<_Ty>>
{
public:
smart_gd_array_ptr(_Ty* _Ptr = 0) : smart_ptr(_Ptr) {}
smart_gd_array_ptr(smart_gd_array_ptr<_Ty>& _Right) : smart_ptr(_Right) {}
smart_gd_array_ptr(smart_ptr<_Ty, global_array_deleter<_Ty>>& _Right) : smart_ptr(_Right) {}

smart_gd_array_ptr<_Ty>& operator = (smart_ptr<_Ty, global_array_deleter<_Ty>>& _Right)
{return (smart_gd_array_ptr<_Ty>&)__super::operator = (_Right);}

smart_gd_array_ptr<_Ty>& operator = (smart_gd_array_ptr<_Ty>& _Right)
{return (smart_gd_array_ptr<_Ty>&)__super::operator = (_Right);}

smart_gd_array_ptr<_Ty>& operator = (_Ty* _Ptr)
{return (smart_gd_array_ptr<_Ty>&)__super::operator = (_Ptr);}

private:
template smart_gd_array_ptr<_Ty> (const smart_ptr<_Ty, _Other>&);
template smart_gd_array_ptr<_Ty>& operator = (const smart_ptr<_Ty, _Other>&);

template smart_gd_array_ptr<_Ty> (const smart_gd_array_ptr<_Other>&);
template smart_gd_array_ptr<_Ty>& operator = (const smart_gd_array_ptr<_Other>&);
};

________________________________________

后记
对于内存管理,其实还有一种情形还没讲的,就是如何优雅地管理 vetor、list、map 这类容器中的指针,这个话题留到以后讨论 STL 时再详细阐述吧。
在本座的代码中基本上看不到 free / delere 这类单词(new 则是有的 —— 给智能指针赋值的时候 ^_^),就本座的经验而言,封装如果利用得当确实能减少很多麻烦,使代码更清晰,有条理,降低错误发生几率。
当然了,封装并不是万能,它不能解决所有问题,关键是靠个人的专注与细心。
本座码字提出自己的观点,旨在抛砖引玉,激发大家思考如何培养良好的编程习惯,不是权威,更不能尽信。最实在的知识应该来自个人最直接的体验。
  敬请期待:《如何养成良好的 C++ 编程习惯(二)—— Windows 资源管理》

摘自 ~怪^_*兽~