Hi All,
Can we delete this pointer from a member function.
I have written three programs to try this. The first two crashes at run time while the third doesn't. Can somebody explain me this?????
1) This crashes at run time.
#include <iostream>
using namespace std;
class Base
{
public:
void temp(void)
{
delete this;
}
};
int main(void)
{
Base x;
x.temp();
return 0;
}
2. This also...
#include <iostream>
using namespace std;
class Base
{
public:
void temp(void)
{
delete this;
}
};
int main(void)
{
Base x;
Base *p = &x;
p->temp();
return 0;
}
3) This doesn't
#include <iostream>
using namespace std;
class Base
{
public:
void temp(void)
{
delete this;
}
};
int main(void)
{
Base *p = new Base;
p->temp();
return 0;
}