運算符重載問題

jiazheng 2008-09-12 11:48:54
#include <iostream>

using namespace std;

class String
{
public:
String(int size=DEFAULTSIZE)
{
m_data=new char[size+1];
}
String (char *strSrc)
{
int size=strlen(strSrc);
assert(size>0);
m_data=new char[size+1];
strcpy(m_data,strSrc);
}
~String()
{
delete []m_data;
}
void Print(void)
{
//delete m_data;
for (int i=0;i<DEFAULTSIZE;i++)
{
printf("%d\n",m_data[i]);
}
cout<<"-----------------------------------------"<<endl;

cout<<m_data<<endl;
}
String& operator = (String &s)
{
if (this==&s)
{
return *this;
}
delete []m_data;
m_data=new char[strlen(s.m_data)+1];
strcpy(m_data,s.m_data);
return *this;
}

friend String operator+ (const String& str1,const String& str2);
private:
char *m_data;
static const int DEFAULTSIZE=10;
};

String operator+ (const String& str1,const String& str2)
{
String temp(strlen(str1.m_data)+strlen(str2.m_data)+1);

strcpy(temp.m_data,str1.m_data);
strcpy(temp.m_data,str2.m_data);

return temp;
}

int main()
{
String a("12345678");
a.Print();
String b;
b=a;
b.Print();

String c;
c=(a+b); //這一句有問題,是什么原因?
c.Print();

system("pause");
return 0;
}
...全文
115 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
太乙 2008-09-12
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wangdeqie 的回复:]
C/C++ code
//改成这样
#include <iostream>
#include <assert.h>
using namespace std;


class String
{
public:
String(int size=DEFAULTSIZE)
{
m_data=new char[size+1];
}
String (char *strSrc)
{
int size=strlen(strSrc);
assert(size>0);
m_data=new char[size+1];
strcpy(m_data,strSrc);
}
~String()

[/Quote]

这可不行,改变了+的本意!
stephen_qi 2008-09-12
  • 打赏
  • 举报
回复
//#include "stdafx.h"
#include <iostream>
#include "assert.h"

using namespace std;
#define DEFAULTSIZE 10
class String
{
public:
String(int size=DEFAULTSIZE)
{
m_data=new char[size+1];
// DEFAULTSIZE=10;
}
String (char *strSrc)
{
// DEFAULTSIZE=10;
int size=strlen(strSrc);
assert(size>0);
m_data=new char[size+1];
strcpy(m_data,strSrc);
}
~String()
{
delete []m_data;
}
void Print(void)
{
//delete m_data;
for (int i=0;i <DEFAULTSIZE;i++)
{
printf("%d\n",m_data[i]);
}
cout <<"-----------------------------------------" <<endl;

cout <<m_data <<endl;
}
String& operator = (String &s)
{
if (this==&s)
{
return *this;
}
delete []m_data;
m_data=new char[strlen(s.m_data)+1];
strcpy(m_data,s.m_data);
return *this;
}

// String operator+(const String& ,const String&);
public:
char *m_data;
//private:
// static int DEFAULTSIZE;
};


String operator+ (const String& str1,const String& str2)
{
String temp(strlen(str1.m_data)+strlen(str2.m_data)+1);
// String temp(str1);
strcpy(temp.m_data,str1.m_data);
strcat(temp.m_data,str2.m_data);
// strcpy(this->m_data,temp.m_data);
// printf("===============");
// temp.Print();
return temp;
}


int main()
{
String a("12345678");
a.Print();
String b;
b=a;
b.Print();

String c;
c=(a+b); //這一句有問題,是什么原因?
c.Print();

system("pause");
return 0;
}

呵呵,来晚了,我的也调成功了!
太乙 2008-09-12
  • 打赏
  • 举报
回复


#include <iostream>
#include <assert.h>
using namespace std;

class String
{
public:
String(int size=DEFAULTSIZE)
{
m_data=new char[size+1];
}
String (char *strSrc)
{
int size=strlen(strSrc);
assert(size>0);
m_data=new char[size+1];
strcpy(m_data,strSrc);
}
String(const String &s)
{
if (this==&s)
{
return ;
}
m_data=new char[strlen(s.m_data)+1];
strcpy(m_data,s.m_data);
}

~String()
{
delete []m_data;
}
void Print(void)
{
//delete m_data;
for (int i=0;i <DEFAULTSIZE;i++)
{
printf("%d\n",m_data[i]);
}
cout <<"-----------------------------------------" <<endl;

cout <<m_data <<endl;
}
String& operator = (String &s)
{
if (this==&s)
{
return *this;
}
delete []m_data;
m_data=new char[strlen(s.m_data)+1];
strcpy(m_data,s.m_data);
return *this;
}

String operator+ (const String& str1);
private:
char *m_data;
static const int DEFAULTSIZE;
};
const int String::DEFAULTSIZE = 10;
String String::operator+ (const String& str2)
{
String temp(strlen(m_data)+strlen(str2.m_data)+1);

strcpy(temp.m_data,m_data);
strcat(temp.m_data,str2.m_data);

return temp;
}

int main()
{
String a("12345678");
a.Print();
String b;
b=a;
b.Print();

String c;
c=(a+b); //這一句有問題,是什么原因?
c.Print();

system("pause");
return 0;
}


wangdeqie 2008-09-12
  • 打赏
  • 举报
回复

//改成这样
#include <iostream>
#include <assert.h>
using namespace std;


class String
{
public:
String(int size=DEFAULTSIZE)
{
m_data=new char[size+1];
}
String (char *strSrc)
{
int size=strlen(strSrc);
assert(size>0);
m_data=new char[size+1];
strcpy(m_data,strSrc);
}
~String()
{
delete []m_data;
}
void Print(void)
{
//delete m_data;
for (int i=0;i <DEFAULTSIZE;i++)
{
printf("%d\n",m_data[i]);
}
cout <<"-----------------------------------------" <<endl;

cout <<m_data <<endl;
}
String& operator = (String &s)
{
if (this==&s)
{
return *this;
}
delete []m_data;
m_data=new char[strlen(s.m_data)+1];
strcpy(m_data,s.m_data);
return *this;
}

String& operator+ (const String& str1)
{
strcat(this->m_data,str1.m_data);
return *this;
}
private:
char *m_data;
static int DEFAULTSIZE;
};
int String::DEFAULTSIZE=0;


int main()
{
String a("12345678");
a.Print();
String b;
b=a;
b.Print();

String c;
c=(a+b); //這一句有問題,是什么原因?
c.Print();

system("pause");
return 0;
}

OenAuth.Core 2008-09-12
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 e_sharp 的回复:]
C/C++ code#include <iostream>

using namespace std;

class String
{
public:
String(int size=DEFAULTSIZE)
{
m_data=new char[size+1];
}

String (char *strSrc)
{
int size=strlen(strSrc);
// assert(size>0);
m_data=new char[size+1];
strcpy(m_data,strSrc);
}

~String()
{
delete []m_da…
[/Quote]


也可以,呵呵,如果类里面有动态char *m_data; 时一定要自己写一个COPY构造函数。不然默认的对象复制是a.m_data=b.m_data这就造成了两个指针指在一个地方,然后a,b都要执行析构函数,这样一个指针会被删除两次,出而出错
OenAuth.Core 2008-09-12
  • 打赏
  • 举报
回复

#include <iostream>
#include <assert.h>
using namespace std;

class String
{
public:
String(int size=DEFAULTSIZE)
{
m_data=new char[size+1];
}

String (char *strSrc)
{
int size=strlen(strSrc);
assert(size>0);
m_data=new char[size+1];
strcpy(m_data,strSrc);
}

~String()
{
delete []m_data;
}

void Print(void)
{
//delete m_data;
for (int i=0;i <DEFAULTSIZE;i++)
{
printf("%d\n",m_data[i]);
}
cout <<"-----------------------------------------" <<endl;

cout <<m_data <<endl;
}


String& operator = (String &s)
{
if (this==&s)
{
return *this;
}
delete []m_data;
m_data=new char[strlen(s.m_data)+1];
strcpy(m_data,s.m_data);
return *this;
}

friend String& operator+ (const String& str1,const String& str2); //用返回引用
private:
char *m_data;
static int DEFAULTSIZE;
};
int String::DEFAULTSIZE=10;

String& operator+ (const String& str1,const String& str2)
{
String temp(strlen(str1.m_data)+strlen(str2.m_data)+1);

strcpy(temp.m_data,str1.m_data);
strcpy(temp.m_data,str2.m_data);

return temp;
}

int main()
{
String a("12345678");
a.Print();
String b;
b=a;
b.Print();

String c;
c=(a+b); //這一句有問題,是什么原因?
c.Print();

system("pause");
return 0;
}

e_sharp 2008-09-12
  • 打赏
  • 举报
回复
#include <iostream> 

using namespace std;

class String
{
public:
String(int size=DEFAULTSIZE)
{
m_data=new char[size+1];
}

String (char *strSrc)
{
int size=strlen(strSrc);
// assert(size>0);
m_data=new char[size+1];
strcpy(m_data,strSrc);
}

~String()
{
delete []m_data;
}
void Print(void)
{
//delete m_data;
for (int i=0;i <DEFAULTSIZE;i++)
{
printf("%d\n",m_data[i]);
}
cout <<"-----------------------------------------" <<endl;

cout <<m_data <<endl;
}

String(const String &s) //编译器自动生产的拷贝构造函数,只是位拷贝,这样m_data只是保存地址,即temp.m_data指向的位置
//temp离开作用域后,temp.m_data就释放了。要拷贝m_data指向的内容,需要自己实现
{
m_data=new char[strlen(s.m_data)+1];
strcpy(m_data,s.m_data);
}

String& operator = (String &s)
{
if (this==&s)
{
return *this;
}
delete []m_data;
m_data=new char[strlen(s.m_data)+1];
strcpy(m_data,s.m_data);
return *this;
}

friend String operator+ (const String& str1,const String& str2);

private:
char *m_data;
static const int DEFAULTSIZE=10;
};

String operator+ (const String& str1,const String& str2)
{
String temp(strlen(str1.m_data)+strlen(str2.m_data)+1);

strcpy(temp.m_data,str1.m_data);
strcpy(temp.m_data,str2.m_data);

return temp; //需要拷贝构造函数
}

int main()
{
String a("12345678");
a.Print();
String b;
b=a;
b.Print();

String c;
c=(a+b); //要用到拷贝构造函数
c.Print();

system("pause");
return 0;
}
jiazheng 2008-09-12
  • 打赏
  • 举报
回复
程序會當掉,高手來解答
self_control 2008-09-12
  • 打赏
  • 举报
回复
我运行了一遍,运行完出了结果之后才出错的...
其实我还是不懂引用传递参数的问题- -||
hai040 2008-09-12
  • 打赏
  • 举报
回复
String& operator = (String &s)
---
String& operator = (const String &s)
jiazheng 2008-09-12
  • 打赏
  • 举报
回复
在dev c++上這種方法可以,在vc8.0上卻有問題
jay的Fans 2008-09-12
  • 打赏
  • 举报
回复
//String& operator = (String &s)
String& operator = (const String &s)
{
if (this==&s)
{
return *this;
}

因为c = (a + b);这句是用a + b运算生成的临时对象给c赋值,也就是你使用了临时对象的引用,这是禁止的,编译不过,
加上const就OK了。

64,637

社区成员

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

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