Jump to content

  • Log In with Google      Sign In   
  • Create Account

xiajia

Member Since 13 Nov 2012
Offline Last Active Jun 06 2013 10:03 PM

#5022230 about "delete" and "delete []"

Posted by xiajia on 16 January 2013 - 11:11 AM

thanks for Brother Bob,Cornstalks and rip-off.These recommendations so refreshing for me.I should solve the problem instead of ducking the issue about double delete.

 

 

you can and should provide it as a regular free function rather than a macro:

Not "macro higher efficiency than function"?




#5022054 about "delete" and "delete []"

Posted by xiajia on 15 January 2013 - 10:45 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?




#5014084 How to use bool variable.

Posted by xiajia on 24 December 2012 - 09:50 PM

yes.if set the warning level to /W4.there will be warning.




#5013880 How to use bool variable.

Posted by xiajia on 24 December 2012 - 12:19 AM

When I use a variable of type bool.People warned me:you'd better written if(a == true) as if(a). I do not know why we must do this. What are the advantages of doing?

My opinion, simply follow the following principles: 'bool' defined variables must be used and can only use 'true' and 'false' initialization and assignment, in the judgment must be written as if (a == true) or if (a== false).
then can avoid all ambiguity and hidden meaning.

Is that true?




#5000735 Try to write a Game engine

Posted by xiajia on 13 November 2012 - 07:13 PM

I am going to write a 2D game engine based on OpenGL, is now complete one part, but encounter many problems, I hope everyone can give some advice.The whole project source code and some examples here.
https://sourceforge.net/projects/xeffect2d/


PARTNERS