复制构造函数的问题,题目中(求助)
查程序错误并改正:
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class strtype
{
char *p;
public:
strtype (char *s); //普通构造函数
~strtype (){delete [] p;}//析构函数
char *get(){retrun p;}
};
strtype::strtype(char *s)//普通构造函数的定义
{
int l;
l = strlen(s) + 1;
p = new char [1];
if (!p)
{
cout<<"Allocations error\n";
exit(1);
}
strcpy (p , s);
}
void show(strtype x)//对象作为函数参数,属于传值调用,需要复制构造函数
{
char *s;
s = x.get();
cout<< s <<"\n";
}
int main()
{
strtype a("hello"),b("there");//这里调用普通构造函数
show(a);//调用复制构造函数,
show(b);//调用复制构造函数,这里输出的应该是乱码。
return 0;
}
以上就是程序代码部分,我想作用就是输出字符串,后面的注释我加的,大家看有错么,这个程序的错误在复制构造函数。
在构造函数后应该加上,自定义复制构造函数。
strtype(const strtype &obj)
{}
在构造时我不会了,书上的例子倒也看懂了,可自己做的时候很乱,没有思路。
2。还有个问题
p = new char [1];这个语句是分配动态内存,我不太明白内存是如何分配的,p是一个指针,给这个指针分配的还是地址上的内存空间分配的,分配的大小是多少,比如p = new char [5],分配的是大小5呢还是分配了5这个数字给p,
先谢谢俄大家了。
国庆大家都怎么过,
问题点数:20、回复次数:10Top
1 楼xiaocai0001(高楼目尽欲黄昏/梧桐叶上萧萧雨)回复于 2005-09-27 19:53:17 得分 0
比如p = new char [5],分配的是大小5呢还是分配了5这个数字给p,
----------
分配的是5个空间Top
2 楼xiaocai0001(高楼目尽欲黄昏/梧桐叶上萧萧雨)回复于 2005-09-27 19:59:35 得分 0
由于类对象数据成员中含有指针
若自己不写复制构造函数,系统会采用位拷贝的方式进行
会出错的Top
3 楼zhouhuahai(道号"虚无")回复于 2005-09-27 20:03:44 得分 0
strtype::strtype(const strtype &obj)
{
p = new char[strlen(obj) + 1];
if (!p)
{
cout<<"Allocations error\n";
exit(1);
}
strcpy(p, obj.p);
}
Top
4 楼xiaocai0001(高楼目尽欲黄昏/梧桐叶上萧萧雨)回复于 2005-09-27 20:05:59 得分 0
虚无怎么乱写程序了!!
p = new char[strlen(obj) + 1];
这个对么??
是
p = new char[strlen(obj.p) + 1];
吧??
Top
5 楼evanmengcn(evan)回复于 2005-09-27 20:26:05 得分 4
在调用show()时,会拷贝构造一个strtype做为参数,这个参数在show()结束的时候会调用~strtype(),这样就会释放掉p的内存。而默认的拷贝构造是memberwise copy的,这样就导致这个释放这个临时参数的时候也释放了原来的参数的内存。
-----------------------------------------------------
to 虚无:
你的拷贝构造函数第一步应该先判断this != &obj罢?Top
6 楼chunhai12(小海)回复于 2005-09-27 20:35:48 得分 4
对于楼主的类,还是重载new、delete方便Top
7 楼mulintaomulintao()回复于 2005-09-27 20:48:01 得分 4
evanmengcn(evan) :你的拷贝构造函数第一步应该先判断this != &obj罢?
拷贝构造函数第一步的第一步是不需要判断的,因为调用时,其对象还没有构建,
判断this != &obj是赋值函数operator=Top
8 楼herorockroll001(默认菜鸟)回复于 2005-09-27 20:57:15 得分 4
虚无的程序:
strtype::strtype(const strtype &obj)
{
p = new char[strlen(obj.p) + 1];//这里给p不用加1了吧,一个地址用得了两个么??
if (!p)
{
cout<<"Allocations error\n";
exit(1);
}
strcpy(p, obj.p);
}Top
9 楼sankt(宠辱不惊,看庭前花开花落;去留无意,望天空云卷云舒.)回复于 2005-09-27 21:58:37 得分 4
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class strtype
{
private:
char *p;
public:
strtype (const char *s); //普通构造函数
strtype(const strtype& other)
{
int len=strlen(other.p);
p=new char[len+1];
strcpy(p,other.p);
}
~strtype ()
{
delete []p;
}//析构函数
char *get()
{
return p;
}
};
strtype::strtype(const char *s)//普通构造函数的定义
{
int n;
n = strlen(s) + 1;
p = new char [n];
if (p==NULL)
{
cout<<"Allocations error\n";
exit(1);
}
strcpy (p , s);
}
void show(strtype x)//对象作为函数参数,属于传值调用,需要复制构造函数
{
char *s;
s = x.get();
cout<< s <<"\n";
}
int main()
{
strtype a("hello"),b("there");//这里调用普通构造函数
show(a);//调用复制构造函数,
show(b);//调用复制构造函数,这里输出的应该是乱码。
return 0;
}
Top
10 楼sankt(宠辱不惊,看庭前花开花落;去留无意,望天空云卷云舒.)回复于 2005-09-27 21:59:01 得分 0
以上测试通过
Top




