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

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

相关文章:

  • 2019-10-23
  • 2021-12-10
  • 2021-08-15
  • 2021-08-22
  • 2022-12-23
  • 2021-08-04
  • 2022-12-23
猜你喜欢
  • 2021-12-11
  • 2022-12-23
  • 2021-06-11
  • 2021-11-18
  • 2021-07-31
  • 2022-12-23
相关资源
相似解决方案