zoukankan      html  css  js  c++  java
  • 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*/

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

  • 相关阅读:
    看代码写程序
    NP
    一道神题
    找平方数
    凝视
    排队打水
    时间计算
    git客户端下载地址
    iOS GCD
    UIView 和 CALayer的那点事
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4833296.html
Copyright © 2011-2022 走看看