Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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 23-Jan-11 14:28pm
malifer337
Edited 24-Jan-11 20:51pm
Indivara33.1K
v2
Comments
Indivara at 25-Jan-11 1:51am
   
[minor edit] Original title: "can someone demonstrate the assignment operator when you need to check for self assignment"
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
malifer at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
}
 
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 270
1 Sergey Alexandrovich Kryukov 185
2 BillWoodruff 173
3 CPallini 155
4 Kamal Rocks 144
0 OriginalGriff 5,655
1 DamithSL 4,506
2 Maciej Los 3,997
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 25 Jan 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100