在赋值操作时,这两种防止自赋值的方法都一要吗?

stevenjin 2009-04-12 01:36:37
if(this == &str) //防止自赋值
{
return *this;
}

改成这样,是一样的吗?
if (this->m_str==str.m_str)
{

return *this;
}

整个代码:

#include "stdafx.h"
#include <iostream>
using namespace std;

class TestString
{
public:
TestString(){};
void Show();
~TestString();
TestString(char* strVlaue);
TestString& operator=(const TestString &testStr);
public:
char* m_str;
private:
};
TestString::~TestString()
{
delete[]m_str;
}
void TestString::Show()
{
cout<<"value="<<m_str<<endl;
}

TestString& TestString::operator =(const TestString &str)
{

if (this->m_str==str.m_str)
{

return *this;
}
this->m_str=new char(strlen(str.m_str)+1);
strcpy(this->m_str,str.m_str);
return *this;
}
TestString::TestString(char *strValue)
{
m_str=strValue;
}
void main()
{
TestString str1;
TestString str2("ok2");
TestString str3;
str1 = str1;
str2.Show();
getchar();
}



...全文
235 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
P_ghost 2009-04-12
  • 打赏
  • 举报
回复
我以前也傻傻的去实现过一个Buffer类,最后才发现,越写下去就越像std::string了。最后就扔了,还是使用std::string
P_ghost 2009-04-12
  • 打赏
  • 举报
回复
为什么要自己去实现标准库里有的东西呢?直接用就OK了std::string
adventurelw 2009-04-12
  • 打赏
  • 举报
回复
两个指针指向同一个地址,那么一定是同一个对象,所以只要比较对象地址(指针)就可以了。
chenlin0604 2009-04-12
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 hairetz 的回复:]
(this == &str) //防止自赋值
{
return *this;
}

改成这样,是一样的吗?
if (this->m_str==str.m_str)
{

return *this;
}


改成这样意思完全变了,前面是完全的自赋值。
而后面的类,可以只跟this共享m_str指针而已。
[/Quote]
什么叫共享指针啊?我真的不太明白啊,共享指针的话,指向的内容不久一样了吗???
谢谢能告诉我一下。
arong1234 2009-04-12
  • 打赏
  • 举报
回复
首先要说明防止自赋值的目的是防止一个对象自己给自己赋值,而不是为了防止两个值一样的不同对象的互相赋值

你这种逻辑上是不一样的,方法1是对象自身的相等,而方法2是对象的成员相等。两者等价的唯一条件就是:如果两个对象的成员相等,那么两个对象必然是同一个(这显然是荒谬的前提)。也就是说:对于你这个特殊的实现,这种方法也许是堆的,但是对于绝大多数的情况,都是不成立的。

举个简单的例子,假如一个表示颜色的类CRgb,他有三个成员是r,g,b分别表示红色、绿色和蓝色。假如我们有个函数GetColor如下
假如我们有两个变量a,b分别表示两个窗口的底色,显然绝大多数窗口底色可能完全一样,因此a.r == b.r; a.g==b.g; a.b==b.b,那么你能说a,b是同一个变量么?显然不是
stevenjin 2009-04-12
  • 打赏
  • 举报
回复
还是不明白,防止自赋值,是防止两个相同的对像赋值呢?还是防止初始化时,两个不同的对像值是一样?
TestString str1("hello");
TestString str2("hello");

str1 = str1; //如这样?
  • 打赏
  • 举报
回复
(this == &str) //防止自赋值
{
return *this;
}

改成这样,是一样的吗?
if (this->m_str==str.m_str)
{

return *this;
}


改成这样意思完全变了,前面是完全的自赋值。
而后面的类,可以只跟this共享m_str指针而已。
mengde007 2009-04-12
  • 打赏
  • 举报
回复
你们一帮人晚上2点还发帖,加油!!
stevenjin 2009-04-12
  • 打赏
  • 举报
回复
再看7楼,您这样写的:

str1 = str2; //But here fail to implement copy

但实际上,已经发生了拷贝。当然str1只是部分被赋值(拷贝函数没对val做赋值操作)。

#include <iostream>

using namespace std;


class TestString
{
public:
TestString()
{
m_str=NULL;
val=90.5;
}
TestString(const char* strVlaue);
void Show();
void setDouble(const double& para);
~TestString();

TestString& operator=(const TestString &testStr);
private:
char* m_str;
double val;
};
TestString::~TestString()
{
if(m_str!=NULL) // Here, you should be careful
delete[]m_str;
}
void TestString::Show()
{
cout<<"value="<<m_str<<endl;
}

void TestString::setDouble(const double& para)
{
val=para;
}

TestString& TestString::operator =(const TestString &str)
{

if(this == &str) //防止自赋值
{
return *this; //Then try this one, It's OK
}
cout<<"Enter copy"<<endl; // for debug
this->m_str=new char[strlen(str.m_str)+1]; ///here
strcpy(this->m_str,str.m_str);
return *this;
}
TestString::TestString(const char *strValue)
{
// m_str=strValue; // here different to: m_str=new char[strlen(strValue)+1];
// But I do think it should be this one, or it could cause shallow copy.
m_str=new char[strlen(strValue)+1];
strcpy(m_str,strValue);
}
int main()
{
TestString str1("ok2");
TestString str2("ok2");
str2.setDouble(65.7); //here make this two different.
//TestString str3;

str1 = str2; //But here fail to implement copy
str2.Show();

system("pause");
return 0;
}
stevenjin 2009-04-12
  • 打赏
  • 举报
回复
谢谢大侠!
不过发现5楼好像有问题,看您写的这一句:
str1 = str2; //Why the strings are the same, but it also copy?
但实际运行时,是这样的:
if (strcmp(this->m_str,str.m_str)==0) //If I use this one, It could be the one you love
{
return *this;//执行时,进入这里,返回了,并没有copy的现像?

}

#include <iostream>

using namespace std;


class TestString
{
public:
TestString()
{
m_str=NULL;
}
TestString(const char* strVlaue);
void Show();
~TestString();

TestString& operator=(const TestString &testStr);
private:
char* m_str;
};
TestString::~TestString()
{
if(m_str!=NULL)
delete[]m_str;
}
void TestString::Show()
{
cout<<"value="<<m_str<<endl;
}

TestString& TestString::operator =(const TestString &str)
{

if (strcmp(this->m_str,str.m_str)==0) //If I use this one, It could be the one you love
{
return *this;
}
cout<<"Enter copy"<<endl; // for debug
this->m_str=new char[strlen(str.m_str)+1]; ///here
strcpy(this->m_str,str.m_str);
return *this;
}
TestString::TestString(const char *strValue)
{
// m_str=strValue; // here different to: m_str=new char[strlen(strValue)+1];
// But I do think it should be this one, or it could cause shallow copy.
m_str=new char[strlen(strValue)+1];
strcpy(m_str,strValue);
}
int main()
{
TestString str1("ok2");
TestString str2("ok2");
//TestString str3;

str1 = str2; //Why the strings are the same, but it also copy?
str2.Show();

system("pause");
return 0;
}
王旺旺旺 2009-04-12
  • 打赏
  • 举报
回复
TestString str1("hello");
TestString str2("hello");

str1 = str2;

很显然不是自赋值,但是你如果用 m_str来判断, ....
HelloDan 2009-04-12
  • 打赏
  • 举报
回复
看一下里面的不同的。各个代码都是有一些细小的差别的。
HelloDan 2009-04-12
  • 打赏
  • 举报
回复

#include <iostream>

using namespace std;


class TestString
{
public:
TestString()
{
m_str=NULL;
val=90.5;
}
TestString(const char* strVlaue);
void Show();
void setDouble(const double& para);
~TestString();

TestString& operator=(const TestString &testStr);
private:
char* m_str;
double val;
};
TestString::~TestString()
{
if(m_str!=NULL) // Here, you should be careful
delete[]m_str;
}
void TestString::Show()
{
cout<<"value="<<m_str<<endl;
}

void TestString::setDouble(const double& para)
{
val=para;
}

TestString& TestString::operator =(const TestString &str)
{

if(this == &str) //防止自赋值
{
return *this; //Then try this one, It's OK
}
cout<<"Enter copy"<<endl; // for debug
this->m_str=new char[strlen(str.m_str)+1]; ///here
strcpy(this->m_str,str.m_str);
return *this;
}
TestString::TestString(const char *strValue)
{
// m_str=strValue; // here different to: m_str=new char[strlen(strValue)+1];
// But I do think it should be this one, or it could cause shallow copy.
m_str=new char[strlen(strValue)+1];
strcpy(m_str,strValue);
}
int main()
{
TestString str1("ok2");
TestString str2("ok2");
str2.setDouble(65.7); //here make this two different.
//TestString str3;

str1 = str2; //But here fail to implement copy
str2.Show();

system("pause");
return 0;
}


HelloDan 2009-04-12
  • 打赏
  • 举报
回复

#include <iostream>

using namespace std;


class TestString
{
public:
TestString()
{
m_str=NULL;
val=90.5;
}
TestString(const char* strVlaue);
void Show();
void setDouble(const double& para);
~TestString();

TestString& operator=(const TestString &testStr);
private:
char* m_str;
double val;
};
TestString::~TestString()
{
if(m_str!=NULL) // Here, you should be careful
delete[]m_str;
}
void TestString::Show()
{
cout<<"value="<<m_str<<endl;
}

void TestString::setDouble(const double& para)
{
val=para;
}

TestString& TestString::operator =(const TestString &str)
{

if (strcmp(this->m_str,str.m_str)==0) //If I use this one, It could be the one you love
{
return *this;
}
cout<<"Enter copy"<<endl; // for debug
this->m_str=new char[strlen(str.m_str)+1]; ///here
strcpy(this->m_str,str.m_str);
return *this;
}
TestString::TestString(const char *strValue)
{
// m_str=strValue; // here different to: m_str=new char[strlen(strValue)+1];
// But I do think it should be this one, or it could cause shallow copy.
m_str=new char[strlen(strValue)+1];
strcpy(m_str,strValue);
}
int main()
{
TestString str1("ok2");
TestString str2("ok2");
str2.setDouble(65.7); //here make this two different.
//TestString str3;

str1 = str2; //But here fail to implement copy
str2.Show();

system("pause");
return 0;
}


HelloDan 2009-04-12
  • 打赏
  • 举报
回复

#include <iostream>

using namespace std;


class TestString
{
public:
TestString()
{
m_str=NULL;
}
TestString(const char* strVlaue);
void Show();
~TestString();

TestString& operator=(const TestString &testStr);
private:
char* m_str;
};
TestString::~TestString()
{
if(m_str!=NULL)
delete[]m_str;
}
void TestString::Show()
{
cout<<"value="<<m_str<<endl;
}

TestString& TestString::operator =(const TestString &str)
{

if (strcmp(this->m_str,str.m_str)==0) //If I use this one, It could be the one you love
{
return *this;
}
cout<<"Enter copy"<<endl; // for debug
this->m_str=new char[strlen(str.m_str)+1]; ///here
strcpy(this->m_str,str.m_str);
return *this;
}
TestString::TestString(const char *strValue)
{
// m_str=strValue; // here different to: m_str=new char[strlen(strValue)+1];
// But I do think it should be this one, or it could cause shallow copy.
m_str=new char[strlen(strValue)+1];
strcpy(m_str,strValue);
}
int main()
{
TestString str1("ok2");
TestString str2("ok2");
//TestString str3;

str1 = str2; //Why the strings are the same, but it also copy?
str2.Show();

system("pause");
return 0;
}


HelloDan 2009-04-12
  • 打赏
  • 举报
回复

#include <iostream>

using namespace std;


class TestString
{
public:
TestString()
{
m_str=NULL;
}
TestString(const char* strVlaue);
void Show();
~TestString();

TestString& operator=(const TestString &testStr);
private:
char* m_str;
};
TestString::~TestString()
{
if(m_str!=NULL)
delete[]m_str;
}
void TestString::Show()
{
cout<<"value="<<m_str<<endl;
}

TestString& TestString::operator =(const TestString &str)
{

if (this->m_str==str.m_str)
{
return *this;
}
cout<<"Enter copy"<<endl; // for debug
this->m_str=new char[strlen(str.m_str)+1]; ///here
strcpy(this->m_str,str.m_str);
return *this;
}
TestString::TestString(const char *strValue)
{
// m_str=strValue; // here different to: m_str=new char[strlen(strValue)+1];
// But I do think it should be this one, or it could cause shallow copy.
m_str=new char[strlen(strValue)+1];
strcpy(m_str,strValue);
}
int main()
{
TestString str1("ok2");
TestString str2("ok2");
//TestString str3;

str1 = str2; //Why the strings are the same, but it also copy?
str2.Show();

system("pause");
return 0;
}


stevenjin 2009-04-12
  • 打赏
  • 举报
回复
能举个例子啊,好像不太懂?
HelloDan 2009-04-12
  • 打赏
  • 举报
回复

而且你这里还有一个问题,因为:char* m_str;
所以你比较的时候只是比较指针,不是里面的内容。可能你应该用这个比较:strcmp()
HelloDan 2009-04-12
  • 打赏
  • 举报
回复
第一个明显好啊。如果你的类中还有其他的元素你第二个就比较不了了,但第一个可以。

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧