Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualxiajia

Posted 15 January 2013 - 10:46 PM

Well, with a specific example to illustrate.Smart pointers.The general method is as follows:

 

 

#define XDELETE(p) { if (p != NULL) { delete p; p = NULL; } }
template<class T> class _XSmartP;
template<class T> class _XBackP 
{ 
private:    
    friend _XSmartP<T>;     
    T *m_p;    
    size_t m_counter;    
    _XBackP(T *p)  
       :m_p(p)  
       ,m_counter(1)     
    {   
        printf("_XBackP constructor called!\n");     
    }     
    ~_XBackP()     
    {         
        XDELETE(m_p);   
        printf( "_XBackP distructor called!\n");     
    } 
};  
template<class T> class _XSmartP 
{ 
public:
    _XSmartP(T *p)
        :m_backP(new _XBackP<T>(p))    
    {   
        printf("_XSmartP constructor called ! use = %d\n",m_backP->m_counter);    
    }      
    _XSmartP(const _XSmartP& temp)  
        :m_backP(temp.m_backP)    
    {
        ++m_backP->m_counter;     
        printf("_XSmartP copy constructor called ! use = %d\n",m_backP->m_counter);    
    }       
    _XSmartP<T>& operator=(const _XSmartP<T>&temp) 
    {   
        if(this == &temp) return *this;  
        ++temp.m_backP->m_counter;   
        if(--m_backP->m_counter == 0)   
        {   
            XDELETE(m_backP);  
        }  
        m_backP = temp.m_backP;  
        return *this;  
    }        
    ~_XSmartP()     
    {   
        printf("_XSmartP distructor called ! use = %d\n",m_backP->m_counter);  
        if(--m_backP->m_counter == 0)   
        {   
            XDELETE(m_backP);  
        }    
    }      
    T *getPtr() const     
    {         
        return 
        m_backP->m_p;     
    }      
    T getVal() const     
    {         
        return *m_backP->m_p;
    }     
    void setVal(T val)     
    {         
        *m_backP->m_p = val;     
    } 
private:     
    _XBackP<T> *m_backP;
};

 

 if use as follow:

 

 

_XSmartP<int> temp(new int[20]);

it will cause errors because of "delete" to "new[]".If you want to avoid this problem, you need to define a similar structure for the array.However, this will have a lot of duplicate code.Is there a way to do it to the best of both worlds?


#1xiajia

Posted 15 January 2013 - 10:45 PM

Well, with a specific example to illustrate.Smart pointers.The general method is as follows:

 

 

template<class T> class _XSmartP;
template<class T> class _XBackP 
{ 
private:    
    friend _XSmartP<T>;     
    T *m_p;    
    size_t m_counter;    
    _XBackP(T *p)  
       :m_p(p)  
       ,m_counter(1)     
    {   
        printf("_XBackP constructor called!\n");     
    }     
    ~_XBackP()     
    {         
        XDELETE(m_p);   
        printf( "_XBackP distructor called!\n");     
    } 
};  
template<class T> class _XSmartP 
{ 
public:
    _XSmartP(T *p)
        :m_backP(new _XBackP<T>(p))    
    {   
        printf("_XSmartP constructor called ! use = %d\n",m_backP->m_counter);    
    }      
    _XSmartP(const _XSmartP& temp)  
        :m_backP(temp.m_backP)    
    {
        ++m_backP->m_counter;     
        printf("_XSmartP copy constructor called ! use = %d\n",m_backP->m_counter);    
    }       
    _XSmartP<T>& operator=(const _XSmartP<T>&temp) 
    {   
        if(this == &temp) return *this;  
        ++temp.m_backP->m_counter;   
        if(--m_backP->m_counter == 0)   
        {   
            XDELETE(m_backP);  
        }  
        m_backP = temp.m_backP;  
        return *this;  
    }        
    ~_XSmartP()     
    {   
        printf("_XSmartP distructor called ! use = %d\n",m_backP->m_counter);  
        if(--m_backP->m_counter == 0)   
        {   
            XDELETE(m_backP);  
        }    
    }      
    T *getPtr() const     
    {         
        return 
        m_backP->m_p;     
    }      
    T getVal() const     
    {         
        return *m_backP->m_p;
    }     
    void setVal(T val)     
    {         
        *m_backP->m_p = val;     
    } 
private:     
    _XBackP<T> *m_backP;
};

 

 if use as follow:

 

 

_XSmartP<int> temp(new int[20]);

it will cause errors because of "delete" to "new[]".If you want to avoid this problem, you need to define a similar structure for the array.However, this will have a lot of duplicate code.Is there a way to do it to the best of both worlds?


PARTNERS