Click here to Skip to main content
15,867,895 members
Please Sign up or sign in to vote.
4.67/5 (3 votes)
See more:
I made a simple class with a int pointer created using new. i handled the assignment operator checking for self assignment and deleting the original object. after asking some questions it turns out in my example both checking for self assignment was unnecessary and deleting was unnecessary which i thought i needed to do. otherwise the pointer would move and id lose the ability to delete that memory. so my practice class was both un useful and disappointing. so now i have reached maximum confusion because all books i read say check for self assignment but my practice class didnt need too. btw the class is posted in the c++ thread called check my assignment operator.

so would someone be kind enough to create a simple class with a assignment operator both when you need to check for self assignment and if you have to delete using something other then a char* or string example. i have a sample with a char* array but could use one that is different.

the fact that sometimes you have to check for self assignment and sometimes you dont and sometimes you have to delete and sometimes you dont is confusing me quite alot.
Posted
Updated 24-Jan-11 19:51pm
v2
Comments
Indivara 25-Jan-11 1:51am    
[minor edit] Original title: "can someone demonstrate the assignment operator when you need to check for self assignment"

 
Share this answer
 
Comments
malifer 23-Jan-11 20:15pm    
ok. i think what confused me was there is like 4 different ways to handle assignment operator and check for self assignment. I think in my original test class I had 2 ways combined into one function then someone else offered but a third way altogether. the c++ faq cut off someone of the class but I think I get it now.
take a look:
template <class T>
class tBuff
{
public:
  tBuff(const T* a=0,const unsigned int n=0){ _buff=0; _size=0; put(a,n); }
  ~tBuff(){ if(_buff) free(_buff); }

  tBuff&  operator = (tBuff& other){ put(other._buff,other._buff?other._size:0); return *this; }

protected:
  void  put(const T* buff,const unsigned int size)
  {
  #ifdef SELF_ASSIGN_TEST
    if(_buff!=buff) // should not be the same buffer
    {
      _size = size;
      if(0<_size)
      {
        _buff = (T*)(_buff?realloc(_buff,sizeof(T)*size):malloc(sizeof(T)*size));
        memcpy(_buff,buff,sizeof(T)*size);
      }
    }
  #else // #ifndef SELF_ASSIGN_TEST
    _size = size;
    if(0<_size)
    {
      _buff = (T*)(_buff?realloc(_buff,sizeof(T)*size):malloc(sizeof(T)*size));
      memcpy(_buff,buff,sizeof(T)*size); // possible crash
    }
  #endif // SELF_ASSIGN_TEST
  }
  
protected:
  T*            _buff;
  unsigned int  _size;
};


#undef SELF_ASSIGN_TEST
{
  static int    __testval[] = { 1,2,3,4,5 };
  tBuff<int>    buff(__testval,sizeof(__testval)/sizeof(__testval[0]));

  buff = buff; // possible crash
}

#define SELF_ASSIGN_TEST
{
  static int    __testval[] = { 1,2,3,4,5 };
  tBuff<int>    buff(__testval,sizeof(__testval)/sizeof(__testval[0]));

  buff = buff; // no crash
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900