首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

哪位高手可以给小弟我解释一下这段代码

2012-02-15 
谁可以给我解释一下这段代码最近都忙着调试程序很久没有编写代码拉请问下面的代码怎么理解问题:1.this&o

谁可以给我解释一下这段代码
最近都忙着调试程序  
很久没有编写代码拉

请问   下面的代码怎么理解
问题:
1.this   ==   &   other   起什么作用
2.如果m_data已经被delete过的,再delete会不会出错
3.this在C++中起什么作用

30分相谢
--------------------------------------------
string   的赋值函数
string&   string::operator=(const   string&   other)
{
      if   (this   ==   &other)
            return   *this;
      delete   []   m_data;
      int   len   =   strlen(other.m_data);
      m_data   =   new   char[len   +   1];
      strcpy(m_data,   other.m_data);
      return   *this;
}      



[解决办法]
string& string::operator=(const string& other)
{
if (this == &other)判断是否自己给自己赋值
return *this;
delete [] m_data;
int len = strlen(other.m_data);
m_data = new char[len + 1];
strcpy(m_data, other.m_data);
return *this;
}
两次delete肯定出错
this当前对象的指针,
[解决办法]
up
this 是调用这个方法对象的指针,
(this == &other)如果传进来的是对象自己的话,就不好delete了

热点排行