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

#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*/

版权声明:本文博主原创文章,博客,未经同意不得转载。

原文地址:https://www.cnblogs.com/gcczhongduan/p/4833296.html