初始化表中的对char*初始化需要析构吗?析构有点小问题

lu161513 2014-07-11 03:03:26

class Teacher{
private:
char *name;
int tid;
Student *student;
public:
Teacher();
Teacher(char*name, int tid, Student *student) :name(name), tid(tid), student(student){};
friend void showInfo(Teacher teacher);
~Teacher(){
cout << "teacher的析构函数。" << endl;
delete[] name;
}
};

这是我的第一种写法。
class Teacher{
private:
char *name;
int tid;
Student *student;
public:
Teacher();
Teacher(char*name, int tid, Student *student) :tid(tid), student(student){
int len = strlen(name);
this->name = new char[len + 1];
strcpy(this->name, name);
};
friend void showInfo(Teacher teacher);
~Teacher(){
cout << "teacher的析构函数。" << endl;
delete[] name;
}
};

以上是我第二种写法。
可是不管哪种写法,这个delete好像都有问题,为什么?
如果不写析构函数,程序运行没有问题。。。
按我的理解第二种中我的char *name 是我自己new开辟的空间,应该要析构啊?
顺便请教下在第一种写法中用初始化表初始char*name是一个怎样的过程,跟我写的第二种一样吗?
谢谢大家
...全文
370 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
lu161513 2014-07-11
  • 打赏
  • 举报
回复
引用 11 楼 TopBand 的回复:
你的代码极度不安全. void showInfo(Teacher teacher); 这里参数应该使用引用, void showInfo(Teacher & teacher); 否则这里会调用一个构造函数, 构造函数写的好没问题, 如果使用默认构造, 那么析构必然导致释放数据错误. 比如: Teacher a; .... 然后 某处调用showInfo(a); 这里会调用构造函数,然后析构, 并且释放了name这个变量的空间. 但后续程序可能还需要a实例中的name变量. 当然, 如果构造函数中name重新分配空间, 那么就没问题. 是否的时候, 最好还是判断一下name是否为空.
谢谢你的回答,很有帮助。确实有的地方传参没有用引用导致多构造了几个对象...java转过来的,的确不习惯。。。 还有请问知道参数列表初始化char*,内部是一个什么样的方式吗?
lu161513 2014-07-11
  • 打赏
  • 举报
回复
引用 13 楼 Cnwanglin 的回复:
我在vs2013下测试过的
谢谢你,我也弄出来了,你的代码中对Teacher的初始化了两次,参数表初始化了一次,构造函数初始化了一次,我试了下,构造函数中初始化时new char数组出来,所以是可以delete[]的,但是使用参数表初始化不知道是什么原理,没法直接delete数组。
Cnwanglin 2014-07-11
  • 打赏
  • 举报
回复
我在vs2013下测试过的
Cnwanglin 2014-07-11
  • 打赏
  • 举报
回复
#include "stdafx.h" #include "stdio.h" #include "stdlib.h" #include <iostream> using namespace std; class Student{ }; class Teacher{ private: char* name; int tid; Student *student; public: Teacher(); Teacher(char* name, int tid, Student *student) :name(name), tid(tid), student(student){ int len = strlen(name); this->name = new char[len + 1]; strcpy(this->name, name); }; friend void showInfo(Teacher teacher); ~Teacher(){ cout << "teacher的析构函数。" << endl; delete[] name; } }; int _tmain(int argc, _TCHAR* argv[]) { Student* ps = new Student(); { char* s = "1234456"; Teacher t(s, 1, ps); } delete ps; system("pause"); return 0; }
TopBand 2014-07-11
  • 打赏
  • 举报
回复
你的代码极度不安全. void showInfo(Teacher teacher); 这里参数应该使用引用, void showInfo(Teacher & teacher); 否则这里会调用一个构造函数, 构造函数写的好没问题, 如果使用默认构造, 那么析构必然导致释放数据错误. 比如: Teacher a; .... 然后 某处调用showInfo(a); 这里会调用构造函数,然后析构, 并且释放了name这个变量的空间. 但后续程序可能还需要a实例中的name变量. 当然, 如果构造函数中name重新分配空间, 那么就没问题. 是否的时候, 最好还是判断一下name是否为空.
lu161513 2014-07-11
  • 打赏
  • 举报
回复
而且5L那个运行会出错啊,删掉析构是可以的。。
lu161513 2014-07-11
  • 打赏
  • 举报
回复
引用 7 楼 Cnwanglin 的回复:
#include "stdafx.h" #include <iostream> using namespace std; class Student{ }; class Teacher{ private: string* name; int tid; Student *student; public: Teacher(); Teacher(string* name, int tid, Student *student) :name(name), tid(tid), student(student){}; friend void showInfo(Teacher teacher); ~Teacher(){ cout << "teacher的析构函数。" << endl; delete[] name; } }; int _tmain(int argc, _TCHAR* argv[]) { Student* ps = new Student(); { string* s = new string[2]; Teacher t(s, 1, ps); } delete ps; system("pause"); return 0; }
谢谢,可是在我的第二种写法里面,我有在构造函数里面new一个char[]数组啊,为何不能Delete []name 掉
赵4老师 2014-07-11
  • 打赏
  • 举报
回复
无new,无delete 无new[],无delete[]
Cnwanglin 2014-07-11
  • 打赏
  • 举报
回复
#include "stdafx.h" #include <iostream> using namespace std; class Student{ }; class Teacher{ private: string* name; int tid; Student *student; public: Teacher(); Teacher(string* name, int tid, Student *student) :name(name), tid(tid), student(student){}; friend void showInfo(Teacher teacher); ~Teacher(){ cout << "teacher的析构函数。" << endl; delete[] name; } }; int _tmain(int argc, _TCHAR* argv[]) { Student* ps = new Student(); { string* s = new string[2]; Teacher t(s, 1, ps); } delete ps; system("pause"); return 0; }
Cnwanglin 2014-07-11
  • 打赏
  • 举报
回复
一个简单的使用原则就是:new 和 delete、new[] 和 delete[] 对应使用。
Cnwanglin 2014-07-11
  • 打赏
  • 举报
回复
#include "stdafx.h" #include <iostream> using namespace std; class Student{ }; class Teacher{ private: string* name; int tid; Student *student; public: Teacher(); Teacher(string* name, int tid, Student *student) :name(name), tid(tid), student(student){}; friend void showInfo(Teacher teacher); ~Teacher(){ cout << "teacher的析构函数。" << endl; delete name; } }; int _tmain(int argc, _TCHAR* argv[]) { Student* ps = new Student(); { string* s = new string("123"); Teacher t(s, 1, ps); } delete ps; system("pause"); return 0; }
幻夢之葉 2014-07-11
  • 打赏
  • 举报
回复
引用 1 楼 Cnwanglin 的回复:
char* name = null; if(name) delete[] name;
++
lu161513 2014-07-11
  • 打赏
  • 举报
回复
引用 2 楼 Cnwanglin 的回复:
delete 不是数组的时候 delete [] 数组指针
我是delete [] name啊,name不是那个char数组首字母指针吗
Cnwanglin 2014-07-11
  • 打赏
  • 举报
回复
delete 不是数组的时候 delete [] 数组指针
Cnwanglin 2014-07-11
  • 打赏
  • 举报
回复
char* name = null; if(name) delete[] name;

64,648

社区成员

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

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