等待高手帮我分析程序中的疑问处!谢谢了!

jackylea 2008-12-23 02:12:44
#include<iostream>
#include<string>
using namespace std;
#ifndef WRONG_H_
#define WRONG_H_
class wrong
{
public:
char *str;

wrong(const char *s);
wrong(const wrong& a);

wrong& operator=(const wrong& a);
wrong();
~wrong();
friend ostream& operator<<(ostream &os,const wrong &st);
private:
//char *str;
int len;

};
#endif


#include <iostream>
#include <cstring>
#include "wrong.h"
using namespace std;
wrong::wrong(const char * s)
{
len = strlen(s);
str = new char[len + 1];
strcpy(str, s);

}//拷贝数据
wrong::wrong(const wrong& a)
{
len=a.len;
str=new char(len+1);
strcpy(str,a.str);
}
wrong& wrong::operator=(const wrong& a)
{
if(this==&a)
return *this;
delete [] str;
len=a.len;
str=new char[len+1];
strcpy(str,a.str);
return *this;
}

wrong::wrong()
{
len =0;
str = new char[len+1];
str[0]='\0';

}

wrong::~wrong()
{
cout<<"这个字符串将被删除:"<<str<<'\n';//为了方便观察结果,特留此行代码。
delete [] str;
}

ostream & operator<<(ostream & os, const wrong & st)
{
os << st.str;
return os;
}

#include <iostream>
#include <stdlib.h>
#include "wrong.h"
using namespace std;
void show_right(const wrong&);
void show_wrong(const wrong);
int main()
{
wrong test1("第一个范例。");
wrong test2("第二个范例。");
wrong test3("第三个范例。");
wrong test4("第四个范例。");
cout<<"下面分别输入三个范例:\n";
cout<<test1.str<<endl;
cout<<test2.str<<endl;
cout<<test3.str<<endl;
wrong* wrong1=new wrong(test1);//为什么不能用wrong* wrong1=new wrong;wrong1=test1;?

cout<<wrong1->str<<endl;
delete wrong1;
cout<<test1.str<<endl;//这句有错了吗?怎么没有反映了?
cout<<"使用正确的函数:"<<endl;
show_right(test2);
cout<<test2.str<<endl;//这里我的理解反而应该是错误的才对,因为在函数调用的时候,并没有调用复制构造函数
//所以在函数调用完成以后就会释放对象所指向的内容,此处应该出现野指针!等待高手
//帮我指点迷津了,谢谢!
cout<<"使用错误的函数:"<<endl;
show_wrong(test2);
cout<<test2.str<<endl; //这里我理解应该是正确的,
wrong wrong2(test3);
cout<<"wrong2: "<<wrong2.str<<endl;
wrong wrong3;
wrong3=test4;
cout<<"wrong3: "<<wrong3.str<<endl;
cout<<"下面,程序结束,析构函数将被调用。"<<endl;
return 0;
}
void show_right(const wrong& a)
{
cout<<a.str<<endl;
}
void show_wrong(const wrong a)
{
cout<<a.str<<endl;
}
...全文
109 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
太乙 2008-12-24
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 jackylea 的回复:]
我删除delete [] str; 其结果也是一样的 不解???


cout < <"使用正确的函数:" < <endl;
show_right(test2);
cout < <test2.str < <endl;//这里我的理解反而应该是错误的才对,因为在函数调用的时候,并没有调用复制构造函数
//所以在函数调用完成以后就会释放对象所指向的内容,此处应该出现野指针!等待高手
//帮我指点迷津了,谢谢!
我这里的解释有什么地方不…
[/Quote]


函数调用完后,会释放指针指向的内容


?????

不会释放的~~哪儿释放了???
jackylea 2008-12-24
  • 打赏
  • 举报
回复
我删除delete [] str; 其结果也是一样的 不解???


cout < <"使用正确的函数:" < <endl;
show_right(test2);
cout < <test2.str < <endl;//这里我的理解反而应该是错误的才对,因为在函数调用的时候,并没有调用复制构造函数
//所以在函数调用完成以后就会释放对象所指向的内容,此处应该出现野指针!等待高手
//帮我指点迷津了,谢谢!
我这里的解释有什么地方不对吗?
void show_right(const wrong& a)
{
cout < <a.str < <endl;
}
这里没调用复制构造函数,函数调用完后,会释放指针指向的内容,为什么cout < <test2.str < <endl;这里没有出现错误呢?
CPlusPlusFans 2008-12-24
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 xiaoyisnail 的回复:]
最关键的错误是:
wrong::wrong(const wrong& a)
{
len=a.len;
str=new char(len+1);str=new char[len+1]
strcpy(str,a.str);
}

[/Quote]
这才是正解,不是析构函数的错
jackylea 2008-12-24
  • 打赏
  • 举报
回复
错误解决了,是我的析构函数错误,现在void show_right(const wrong& a)
{
cout < <a.str < <endl;
}
函数的参数使用感到疑惑?谁帮忙解决我的注释处的疑问???
deerwin1986 2008-12-23
  • 打赏
  • 举报
回复
test2又不是函数内定义的临时变量 不会释放
xiaoyisnail 2008-12-23
  • 打赏
  • 举报
回复
最关键的错误是:
wrong::wrong(const wrong& a)
{
len=a.len;
str=new char(len+1); str=new char[len+1]
strcpy(str,a.str);
}
xiaoyisnail 2008-12-23
  • 打赏
  • 举报
回复
//为什么不能用wrong* wrong1=new wrong;wrong1=test1;?
wrong1是指针,当然不能用test1赋值啊
jackylea 2008-12-23
  • 打赏
  • 举报
回复
没人帮忙看吗 顶下
sincor 2008-12-23
  • 打赏
  • 举报
回复
#include <iostream> 
#include <string>
using namespace std;
#ifndef WRONG_H_
#define WRONG_H_
class wrong
{
public:
char *str;

wrong(const char *s);
wrong(const wrong& a);

wrong& operator=(const wrong& a);
wrong();
~wrong();
friend ostream& operator < <(ostream &os,const wrong &st);
private:
//char *str;
int len;

};
#endif


#include <iostream>
#include <cstring>
#include "wrong.h"
using namespace std;
wrong::wrong(const char * s)
{
len = strlen(s);
str = new char[len + 1];
strcpy(str, s);

}//拷贝数据
wrong::wrong(const wrong& a)
{
len=a.len;
str=new char(len+1);
strcpy(str,a.str);
}
wrong& wrong::operator=(const wrong& a)
{
if(this==&a)
return *this;
delete [] str;
len=a.len;
str=new char[len+1];
strcpy(str,a.str);
return *this;
}

wrong::wrong()
{
len =0;
str = new char[len+1];
str[0]='\0';

}

wrong::~wrong()
{
cout < <"这个字符串将被删除:" < <str < <'\n';//为了方便观察结果,特留此行代码。
delete [] str;
}

ostream & operator < <(ostream & os, const wrong & st)
{
os < < st.str;
return os;
}

#include <iostream>
#include <stdlib.h>
#include "wrong.h"
using namespace std;
void show_right(const wrong&);
void show_wrong(const wrong);
int main()
{
wrong test1("第一个范例。");
wrong test2("第二个范例。");
wrong test3("第三个范例。");
wrong test4("第四个范例。");
cout < <"下面分别输入三个范例:\n";
cout < <test1.str < <endl;
cout < <test2.str < <endl;
cout < <test3.str < <endl;
wrong* wrong1=new wrong(test1);//为什么不能用wrong* wrong1=new wrong;wrong1=test1;?

cout < <wrong1->str < <endl;
delete wrong1;
cout < <test1.str < <endl;//这句有错了吗?怎么没有反映了?
cout < <"使用正确的函数:" < <endl;
show_right(test2);
cout < <test2.str < <endl;//这里我的理解反而应该是错误的才对,因为在函数调用的时候,并没有调用复制构造函数
//所以在函数调用完成以后就会释放对象所指向的内容,此处应该出现野指针!等待高手
//帮我指点迷津了,谢谢!
cout < <"使用错误的函数:" < <endl;
show_wrong(test2);
cout < <test2.str < <endl; //这里我理解应该是正确的,
wrong wrong2(test3);
cout < <"wrong2: " < <wrong2.str < <endl;
wrong wrong3;
wrong3=test4;
cout < <"wrong3: " < <wrong3.str < <endl;
cout < <"下面,程序结束,析构函数将被调用。" < <endl;
return 0;
}
void show_right(const wrong& a)
{
cout < <a.str < <endl;
}
void show_wrong(const wrong a)
{
cout < <a.str < <endl;
}
CPlusPlusFans 2008-12-23
  • 打赏
  • 举报
回复
mark

65,210

社区成员

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

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