初学C++,有点类数组定义的问题不太明白

HEHE8737 2011-08-09 03:05:37
以下是我刚看完类与对象这章写的小程序,发现以下注释掉的类初始化会导致析构函数double free or corruption (fasttop): 0x08c80038 *** ,编译环境linux g++。VC能运行,但是name打印出乱码,用没有注释掉的定义方式是可行的。我刚看c++几天,问的太弱希望别见怪。。。
#include<iostream>
#include<string.h>
using namespace std;

class Student
{
int age;
char *name;
public:
Student(int,char *);
Student();
~Student();
void SetName(int,char *);
int Getage(){return age;}
char *GetName(){return name;}
};

Student::Student(int m,char *n)
{
cout<<"Paramater!"<<endl;
age=m;
name=new char[strlen(n)+1];
strcpy(name,n);
}
Student::Student()
{
cout<<"No paramater!"<<endl;
age=0;
char *n="baolun";
name=new char[strlen(n)+1];
strcpy(name,n);
}
Student::~Student()
{
cout<<"Free student!"<<endl;
delete []name;
}
void Student::SetName(int m ,char *n)
{
age=m;
name=new char(strlen(n)+1);
strcpy(name,n);
}


main()
{
int i;
/* Student stu[3];
stu[0]=Student();
stu[1]=Student(21,"yanhufen");
stu[2]=Student(19,"baocaomin");*/
Student stu[3]=
{
Student(),
Student(31,"sldjfl"),
Student(19,"baocaomin")
};

for(i=0;i<3;i++)
{
cout<<"Stu["<<i<<"]: ";
cout<<"Name:"<<stu[i].GetName()<<" ";
cout<<"Age:"<<stu[i].Getage()<<endl;

}
}
...全文
92 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
HEHE8737 2011-08-09
  • 打赏
  • 举报
回复
谢谢newfarmerchi!
newfarmerchi 2011-08-09
  • 打赏
  • 举报
回复
第一种方法是先定义一个对象数组,每个对象调用
的都是Student()来初始化的。然后再用重载后的
赋值运算符给每个对象赋新的值。
第二种方法是定义对象数组的同时用不同的构造函数来
初始化。
HEHE8737 2011-08-09
  • 打赏
  • 举报
回复
二楼的大侠说的 是正确的,但是这两种定义方式有什么区别吗?我很想知道。。。
newfarmerchi 2011-08-09
  • 打赏
  • 举报
回复

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

class Student
{
int age;
char *name;
public:
Student(int,char *);
Student();
Student(const Student &);//<------增加一个拷贝构造函数
~Student();
void SetName(int,char *);
int Getage(){return age;}
char *GetName(){return name;}
const Student &operator= (const Student &);//<--赋值运算符重载
};

Student::Student(int m,char *n)
{
cout<<"Paramater!"<<endl;
age=m;
name=new char[strlen(n)+1];
strcpy(name,n);
}
Student::Student()
{
cout<<"No paramater!"<<endl;
age=0;
char *n="baolun";
name=new char[strlen(n)+1];
strcpy(name,n);
}
Student::Student(const Student &t)//<----------here
{
age=t.age;
name=new char[strlen(t.name)+1];
strcpy(name,t.name);
}
Student::~Student()
{
cout<<"Free student!"<<endl;
delete []name;
}
void Student::SetName(int m ,char *n)
{
age=m;
name=new char(strlen(n)+1);
strcpy(name,n);
}
const Student &Student::operator= (const Student &Temp)//<--here
{
if (&Temp != this)
{
age=Temp.age;
delete [] name;
name=new char[strlen(Temp.name)+1];
strcpy(name,Temp.name);
}
return *this;
}


main()
{
int i;
Student stu[3];
stu[0]=Student();
stu[1]=Student(21,"yanhufen");
stu[2]=Student(19,"baocaomin");
/*Student stu[3]=
{
Student(),
Student(31,"sldjfl"),
Student(19,"baocaomin")
};*/
for(i=0;i<3;i++)
{
cout<<"Stu["<<i<<"]: ";
cout<<"Name:"<<stu[i].GetName()<<" ";
cout<<"Age:"<<stu[i].Getage()<<endl;

}
}



nightkids_008 2011-08-09
  • 打赏
  • 举报
回复
* Student stu[3];
stu[0]=Student();
stu[1]=Student(21,"yanhufen");
stu[2]=Student(19,"baocaomin");
这几句全不要,就留最后一个初始化语句就行了。

64,648

社区成员

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

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