【发布时间】:2016-11-19 05:06:03
【问题描述】:
我已经声明了一个带有构造函数和析构函数的简单类。但是,当我删除对象时,它会给出runtime error 并且不再执行输出。
class Student {
public:
string name;
Student(string name) {
this->name=name;
}
~Student() {
this->name="";
}
};
int main() {
Student* s = new Student("a");
cout<<s->name<<endl;
delete s; /// Problem In This Line
cout<<"Name Here -> "<<s->name<<endl;
return 0;
}
我的问题是什么?我应该如何删除或调用析构函数?
【问题讨论】:
-
删除指针后就不能使用了。
-
谢谢。我得到了它。 @代码学徒
标签: c++ destructor