String * operator +(const String &s1, const String &s2)

zjj2282680 2010-03-26 09:04:24
class String
{
public:
String(const char *str = NULL); // 普通构造函数
String(const String &other); // 拷贝构造函数
~String(void); // 析构函数
String & operator =(const String &other); // 赋值函数
friend String * operator +(const String &str1, const String &str2);
char *GetString(void);//{return m_data;}
private:
char *m_data; // 用于保存字符串

};

String * operator +(const String &s1, const String &s2)
{
String *temp;
delete []temp->m_data; // temp.data是仅含'\0'的字符串
temp->m_data = new char[strlen(s1.m_data) + strlen(s2.m_data) +1];
strcpy(temp->m_data, s1.m_data);
strcat(temp->m_data, s2.m_data);
return temp;
}

int main(int argc, char* argv[])
{
String str1 = String("Hello World!");
cout<<str1.GetString()<<endl;

String str2 = String(str1);
cout<<str2.GetString()<<endl;

String *str5;
str5 = str1 + str2; //为什么会出错?
cout<<str5->GetString()<<endl;
return 0;
}
...全文
615 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjj2282680 2010-03-26
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 joey_zoy 的回复:]
String *temp = new String();
delete []temp->m_data;

这样改一下
[/Quote]

谢谢 你是对的
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 zjj2282680 的回复:]
引用 2 楼 gigglesun 的回复:
不要返回局部对象的引用或指针;temp指向的对象在返回时已经删除了


我这个是指针函数 返回的是指针地址 不是引用 呵呵
[/Quote]
不管是指針還是引用。。。你這個不是用new分配的零時變量。。。在函數返回時都會被銷毀。。。所以。。你返回的指針是不對的。。
  • 打赏
  • 举报
回复
String *temp;
delete []temp->m_data;//

感觉这句话会出问题...temp是個局部變量...最後你返回的是一個局部變量。。。指針被釋放。。。所以會出錯。。
你看遮樣。。行不行。。
String *temp = new String;
temp->m_data = new char[strlen(s1.m_data) + strlen(s2.m_data) +1];
strcpy(temp->m_data, s1.m_data);
strcat(temp->m_data, s2.m_data);
return temp;

joey_zoy 2010-03-26
  • 打赏
  • 举报
回复

String *temp = new String();
delete []temp->m_data;

这样改一下
joey_zoy 2010-03-26
  • 打赏
  • 举报
回复
String *temp;
delete []temp->m_data;
temp是指针,没有初始化,调用delete肯定会出错
zjj2282680 2010-03-26
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 ypb362148418 的回复:]
看出来了,
String *temp;
delete []temp->m_data;//temp被删除了
你应该构造一个对象然后返回
[/Quote]

temp 没被删,被删的是temp->m_data做占用的内存
temp->m_data = new char[strlen(s1.m_data) + strlen(s2.m_data) +1];
这句话又给temp->m_data重新申请内存了。。。
zjj2282680 2010-03-26
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 joey_zoy 的回复:]
算术操作符返回对象,赋值操作符返回引用

C/C++ code

class mystring
{
public:
mystring(const TCHAR *pStr = NULL);
~mystring();
mystring(const mystring &other);
mystring &operator =(const……
[/Quote]

呵呵 谢谢你的代码
但我的问题是 我这段程序为什么运行会出错?
ypb362148418 2010-03-26
  • 打赏
  • 举报
回复
看出来了,
String *temp;
delete []temp->m_data;//temp被删除了
你应该构造一个对象然后返回
RubishHunter 2010-03-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 zjj2282680 的回复:]
引用 2 楼 gigglesun 的回复:
不要返回局部对象的引用或指针;temp指向的对象在返回时已经删除了


我这个是指针函数 返回的是指针地址 不是引用 呵呵
[/Quote]

temp并没有初始化,所以你根本不知道他指向哪里啊?
进行Delete操作有什么意义?
更别说给他的成员赋值了!
zjj2282680 2010-03-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ypb362148418 的回复:]
错误是什么,贴出来看看。
String & operator =(const String &other); // 赋值函数
friend String * operator +(const String &str1, const String &str2);
//为什么你这一块要用friend
看着没貌似没错误
[/Quote]

运行错误,没提到什么错。。。
用friend 才能带2个参数
不信你可以试试 把friend 去掉 会报 参数太多 的错误
zjj2282680 2010-03-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 gigglesun 的回复:]
不要返回局部对象的引用或指针;temp指向的对象在返回时已经删除了
[/Quote]

我这个是指针函数 返回的是指针地址 不是引用 呵呵
zjj2282680 2010-03-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yuzl32 的回复:]
String *temp;
delete []temp->m_data; // temp还没分配.
[/Quote]

temp->m_data = new char[strlen(s1.m_data) + strlen(s2.m_data) +1];
这句话 已经分配内存了
joey_zoy 2010-03-26
  • 打赏
  • 举报
回复
算术操作符返回对象,赋值操作符返回引用

class mystring
{
public:
mystring(const TCHAR *pStr = NULL);
~mystring();
mystring(const mystring &other);
mystring &operator =(const mystring &other);
mystring &operator +=(const mystring &other);
friend const mystring operator+(const mystring &lhs, const mystring &rhs);
private:
TCHAR *mData;
};

mystring::mystring(const TCHAR *pStr /* = NULL */)
{
if(!pStr)
{
mData = new TCHAR[1];
*mData = '\0';
}
else
{
mData = new TCHAR[strlen(pStr)+1];
strcpy(mData,pStr);
}
}

mystring::~mystring()
{
delete[] mData;
}

mystring::mystring(const mystring &other)
{
mData = new TCHAR[strlen(other.mData)+1];
strcpy(mData,other.mData);
}

mystring& mystring::operator =(const mystring &other)
{
/*if(this != &other)
{
delete []mData;
mData = new TCHAR[strlen(other.mData)+1];
strcpy(mData,other.mData);
}
*/
TCHAR *OriginalData = mData;
mData = new TCHAR[strlen(other.mData)+1];
strcpy(mData,other.mData);
delete []OriginalData;
return *this;
}


mystring& mystring::operator +=(const mystring &other)
{
TCHAR *OriginalData = mData;
mData = new TCHAR[strlen(OriginalData) +strlen(other.mData)+1];
strcpy(mData,OriginalData);
strcat(mData,other.mData);
delete []OriginalData;
return *this;
}

const mystring operator+(const mystring &lhs, const mystring &rhs)
{
mystring temp(lhs);
temp += rhs;
return temp;
}
chenyu2202863 2010-03-26
  • 打赏
  • 举报
回复
friend String * operator +(const String &s1, const String &s2)


==>
一般重载符函数都为成员函数,而不是友元
ypb362148418 2010-03-26
  • 打赏
  • 举报
回复
错误是什么,贴出来看看。
String & operator =(const String &other); // 赋值函数
friend String * operator +(const String &str1, const String &str2);
//为什么你这一块要用friend
看着没貌似没错误
ithiker 2010-03-26
  • 打赏
  • 举报
回复
不要返回局部对象的引用或指针;temp指向的对象在返回时已经删除了
yuzl32 2010-03-26
  • 打赏
  • 举报
回复
String *temp;
delete []temp->m_data; // temp还没分配.

65,202

社区成员

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

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