While tracing the code given below. The code works fine on a single call to void Function()
. It prints the respective values of x
and y
. But when I invoke the same function again, it throws me a runtime error. I see the usage of delete this
in the function.
1. Does it mean that allocated stack of that function is freed?
2. What does delete this
mean & Why the program throws runtime error?
#include<iostream>
using namespace std;
class Point
{
int x;
float y;
public:
void Function()
{
x = 6;
y = 7.50;
delete this;
}
void Display()
{
cout<< x << " " << y;
}
};
int main()
{
Point *ptr = new Point();
ptr->Function();
ptr->Function(); //Second call throws an error
ptr->Display();
return 0;
}