重载operator = ,出现了莫名其妙的问题

knift1128 2008-05-23 11:29:08
代码如下:


//IsEcho.h
#include <iostream>

class IsEcho
{
private:
const int BUFSIZE;
char *pStr;
public:
IsEcho(const char *p = 0);
IsEcho(const IsEcho &);
IsEcho & operator = (const IsEcho &);
virtual ~IsEcho();
void say();
};




//IsEcho.cpp
#include "IsEcho.h"

using namespace std;
IsEcho::IsEcho(const char *p):BUFSIZE(1024)
{
cout << "this is IsEcho()" << endl;
this->pStr = new char[this->BUFSIZE];
strcpy(this->pStr,p);
}
IsEcho::IsEcho(const IsEcho& is):BUFSIZE(1024)
{
cout << "this is IsEcho(IsEcho &)" << endl;
this->pStr = new char[this->BUFSIZE];
strcpy(this->pStr,is.pStr);
}
IsEcho::~IsEcho()
{
cout << "this is ~IsEcho()" << endl;
delete this->pStr;
}
IsEcho& IsEcho::operator =(const IsEcho& is)
{
cout << "this is operator = " << endl;
this->pStr = new char[this->BUFSIZE];
strcpy(this->pStr,is.pStr);
return *this;
}

void IsEcho::say()
{
cout << this->pStr << endl;
}

#include "IsEcho.h"


using namespace std;

int main()
{
{
IsEcho is1("test");
is1.say();
IsEcho is2(is1);
is2.say();
IsEcho is3 = is2;
is3.say();
}
system("pause");
return 0;
}


在IsEcho is3 = is2;这里走单步发现根本就不进IsEcho& IsEcho::operator =(const IsEcho& is),而是进了拷贝构造,operator =要调用拷贝构造倒是正常,但这个代码根本就不进operator =
这是何故??环境VS2008
解决了马上结帖~
...全文
91 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ymntomyimi8855 2008-05-23
  • 打赏
  • 举报
回复
第一:IsEcho is3 = is2
执行的应该是构造函数
第二:IsEcho& IsEcho::operator =(const IsEcho& is)
{
cout << "this is operator = " << endl;
this->pStr = new char[this->BUFSIZE];
strcpy(this->pStr,is.pStr);
return *this;
}
这个函数定义错误; this->pStr = new char[this->BUFSIZE];
改成this->pStr = new char[is.BUFSIZE];
再加一个条if(*this!=is)
knift1128 2008-05-23
  • 打赏
  • 举报
回复
明白了!谢谢大家!!已结帖给分
逸学堂 2008-05-23
  • 打赏
  • 举报
回复
IsEcho is3 = is2;
这种方式,C++编译器会自动优化成IsEcho is3(is2);所以不会调用operator=.
PcrazyC 2008-05-23
  • 打赏
  • 举报
回复
你这是初始化不是赋值当然执行拷贝构造函数了,你改一下,改成这样就行了,下面的才是赋值


IsEcho is3;
is3=is2;
xkyx_cn 2008-05-23
  • 打赏
  • 举报
回复

#include "IsEcho.h"


using namespace std;

int main()
{
{
IsEcho is1("test");
is1.say();
IsEcho is2(is1);
is2.say();
IsEcho is3 = is2; // 这样是构造一个对象,不需要用到赋值,编译器帮你简化了
is3.say();
}
system("pause");
return 0;
}

knift1128 2008-05-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hai040 的回复:]
试试
IsEcho is3("");
is3 = is2;
[/Quote]


这样是正常的
xkyx_cn 2008-05-23
  • 打赏
  • 举报
回复


#include "IsEcho.h"


using namespace std;

int main()
{
{
IsEcho is1("test");
is1.say();
IsEcho is2(is1);
is2.say();
IsEcho is3;
is3 = is2; // 这样会进赋值
is3.say();
}
system("pause");
return 0;
}


而且你的赋值函数最好判断一下是不是自赋值:


IsEcho& IsEcho::operator =(const IsEcho& is)
{
cout << "this is operator = " << endl;

if (*this == is) {
return *this;
}

this->pStr = new char[this->BUFSIZE];
strcpy(this->pStr,is.pStr);
return *this;
}


hai040 2008-05-23
  • 打赏
  • 举报
回复
试试
IsEcho is3("");
is3 = is2;

64,653

社区成员

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

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