- template<typename _PointerTy_>
- class _CPointer
- {
- public:
- //..构造函数
- _CPointer(_PointerTy_ * _InPtr );
- //..析构函数
- virtual ~ _CPointer();
- //..重载*寻址运算符
- virtual _PointerTy_ & operator *();
- //..重载->运算符
- virtual _PointerTy_ * operator ->();
- protected:
- _PointerTy_* m_Ptr;
- };
- //==============================================================//
- //.._StartPointer类实现
- //..构造函数实现
- template<typename _PointerTy_>
- ::_CPointer< _PointerTy_>::_CPointer (_PointerTy_* _InPtr)
- {
- this->m_Ptr = _InPtr;
- }
- //..析构函数实现
- template<typename _PointerTy_>
- ::_CPointer< _PointerTy_>::~_CPointer ()
- {
- delete this ->m_Ptr;
- this->m_Ptr = nullptr;
- }
- //..operator*()实现
- template<typename _PointerTy_>
- _PointerTy_& _CPointer<_PointerTy_ >::operator*()
- {
- return *( this->m_Ptr );
- }
- //..operator->()实现
- template<typename _PointerTy_>
- _PointerTy_* _CPointer<_PointerTy_ >::operator->()
- {
- /* 因为特殊原因,所以C++中的Obj->set(5)等价于(Obj.operator->())->set(5)
- * C++会给重载operator->返回的任何结果隐式地再应用一次operator->,所以自定义
- * 类中的operator->()必须返回一个指针,而不是引用 */
- return this ->m_Ptr;
- }
- //该片段来自于http://www.codesnippet.cn/detail/130120148515.html
来源: http://www.codesnippet.cn/detail/130120148515.html