操作员检查自我分配 [英] Operator to check for self assignment

查看:84
本文介绍了操作员检查自我分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用一个用new创建的int指针做了一个简单的类.我处理了赋值运算符,检查自我赋值并删除原始对象.在问了一些问题之后,在我的示例中发现我既不需要检查自我分配,也不需要删除,这是我认为我需要做的.否则,指针将移动并且id失去删除该内存的能力.所以我的练习课没有用,也很令人失望.所以现在我已经感到最大的困惑,因为我读过的所有书都说要检查自我作业,但是我的练习课也不需要.顺便说一句,该类已发布在名为检查我的赋值运算符"的c ++线程中.

因此,当您需要检查自我分配以及是否必须使用char *或字符串示例以外的其他内容删除时,有人会足够友好地使用赋值运算符创建一个简单的类.我有一个带有char *数组的示例,但可以使用一个不同的示例.

有时您必须检查自我分配,有时您不必检查,有时您必须删除,有时却不这样做的事实使我非常困惑.

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.

推荐答案

此处提供一些示例: http://www.parashift.com/c++-faq-lite/assignment- operator.html [^ ]
There are some examples here: http://www.parashift.com/c++-faq-lite/assignment-operators.html[^]


看看:
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
}


这篇关于操作员检查自我分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆