哪位高手可以给小弟我解释一下这段代码
谁可以给我解释一下这段代码
最近都忙着调试程序
很久没有编写代码拉
请问 下面的代码怎么理解
问题:
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了
最近都忙着调试程序
很久没有编写代码拉
请问 下面的代码怎么理解
问题:
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了