jbsu32 jbsu32 - 11 months ago 55
C++ Question

How to delete this object?

I have declared a simple class with constructor and destructor. But, when I delete the object, it gives

runtime error
and executes no further output.

class Student {
string name;

Student(string name) {

~Student() {


int main() {
Student* s = new Student("a");
delete s; /// Problem In This Line
cout<<"Name Here -> "<<s->name<<endl;
return 0;

What is my problem here?? How should I delete or call the destructor??

Answer Source

After you delete a pointer you cannot use it. If you want to show that your destructor worked, put the cut statement inside it rather than in main().