注册 登录
编程论坛 C++教室

String类的+运算怎么出错了?

hwdwow 发布于 2009-09-27 10:51, 671 次点击

String operate+(const String &s1, const String &s2)

{
   
    String temp;
   
    delete temp.data; // temp.data是仅含‘\0’的字符串
   
    temp.data = new char[strlen(s1.data) + strlen(s2.data) +1];
   
    strcpy(temp.data, s1.data);
   
    strcat(temp.data, s2.data);
   
    return temp;
   
}
4 回复
#2
微软MVP2009-10-01 18:57
不是已经被delete了吗?
#3
a26960262009-10-01 21:58
我学的是先有new   然后再delete

只有先建立 再删除  是不是?


楼下的回复下   楼主也回复啊。。。。。。。。。。。。。。。。。。。

#4
hwdwow2009-10-03 23:06
operate operator???似乎是低级错误
#5
xxcc3092009-10-07 17:01
只有被动态分配的对象才能被DELETE也就是你说的只有是先经过NEW的才能DELETE.
1