I was just toying around idea of deleting this from a member function.
Was expecting that any acess to member variable or function after
deleting sould give me dump(segmetation violation).Cause now I am
trying to access freed.
memory.
Is my assumption correct?
I tried following code
---------------------------------------------------
#include <iostream.h>
class A
{
public:
A():_i(0){}
A(int i_):_i(i_){}
void killself(){cout<<"deleting this"<<endl;delete this;A*
test = ((A*)t
his);test=0;_i = 5;}
int getI(){return _i;}
void setI(int i_=0){_i= i_;}
private:
int _i;
};
int main()
{
A a;
a.setI(10);
cout<<a.getI()<<endl;
a.killself();
cout<<a.getI()<<endl;
A* pa = NULL;
pa = new A(10);
cout<<pa->getI()<<endl;
cout<<"pointer value"<<pa<<endl;
pa->killself();
cout<<pa->getI()<<endl;
cout<<"pointer value"<<pa<<endl;
delete pa;
pa = NULL;
return 0;
}
------------------------------------------
As I was expecting msvc6 gave dump at statement "a.killself();".But if
I comment this line I get dump at "delete pa;". I am unable to
understand the behaviour.Can somebody help me understand what's going
on?
I think there was thread on similar topic, but I m unable to locate
it.Would even appreciate if get link for same.
Regards,
Abhijeet
Ps. - If I comiple it with "gcc version 3.3.1 (cygming special)" code
runs perfectly fine without any dump. Same is case with Sun workshop