Smart pointer template for managing dynamically allocated memory

Source: Internet
Author: User

Smart pointer template for managing dynamically allocated memory

#ifndef SMARTPTR_HPP#define SMARTPTR_HPP #include 
 
  template 
  
   class SmartPtr{    public:        SmartPtr(T *type = NULL);        void resetPtr(T *type);        const T *getPtr()const;        operator bool() const{            return ptr_ == NULL;        }        ~SmartPtr();        T &operator*();        const T &operator*()const;        T *operator->();        const T *operator->()const;    private:        SmartPtr(const SmartPtr &);        void operator=(const SmartPtr &);        T *ptr_;};template 
   
    inline SmartPtr
    
     ::SmartPtr(T *type)    :ptr_(type){}template 
     
      inline void SmartPtr
      
       ::resetPtr(T *type){ if(ptr_ != type){ if(ptr_ != NULL){ delete ptr_; } ptr_ = type; }}template 
       
        inline const T *SmartPtr
        
         ::getPtr() const{ return ptr_;}template 
         
          inline SmartPtr
          
           ::~SmartPtr(){ if(ptr_ != NULL){ delete ptr_; }}template 
           
            inline T &SmartPtr
            
             ::operator*(){ return *ptr_;}template 
             
              inline const T &SmartPtr
              
               ::operator*() const{ return *ptr_;}template 
               
                inline T *SmartPtr
                
                 ::operator->(){ return ptr_;}template 
                 
                  inline const T *SmartPtr
                  
                   ::operator->() const{ return ptr_;}#endif /*SMARTPTR_H*/
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.