jbsu32 jbsu32 - 1 year ago 92
C++ Question

C++: 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().

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download