您的位置:首页 > 编程语言 > C语言/C++

C++的一个简单的句柄类模板

2014-07-23 01:21 302 查看
#ifndef HANDLE_H
#define HANDLE_H
#include "Animal.h"

template <typename T>
class Handle{
public:
Handle(T *ptr);
Handle(const Handle &other);
Handle &operator = (const Handle &other);
~Handle();
T *operator->();
private:
T *ptr_;
};

template <typename T>
inline Handle<T>::Handle(T *ptr)
:ptr_(ptr->copy())
{}

template <typename T>
inline Handle<T>::Handle(const Handle &other)
:ptr_(other.ptr_->copy())
{}

template <typename T>
inline Handle<T> &Handle<T>::operator = (const Handle &other)
{
if(this != &other){
delete ptr_;
ptr_ = other.ptr_->copy();
}
return *this;
}

template <typename T>
inline Handle<T>::~Handle()
{
delete ptr_;
}

template <typename T>
inline T *Handle<T>::operator -> ()
{
return ptr_;
}
#endif  /*HANDLE_H*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ 句柄类 模板