C++的一個簡單的句柄類模板
#ifndef HANDLE_H
#define HANDLE_H
#include "Animal.h"
template
class Handle{
public:
Handle(T *ptr);
Handle(const Handle &other);
Handle &operator = (const Handle &other);
~Handle();
T *operator->();
private:
T *ptr_;
};
template
inline Handle::Handle(T *ptr)
:ptr_(ptr->copy())
{}
template
inline Handle::Handle(const Handle &other)
:ptr_(other.ptr_->copy())
{}
template
inline Handle &Handle::operator = (const Handle &other)
{
if(this != &other){
delete ptr_;
ptr_ = other.ptr_->copy();
}
return *this;
}
template
inline Handle::~Handle()
{
delete ptr_;
}
template
inline T *Handle::operator -> ()
{
return ptr_;
}
#endif /*HANDLE_H*/