H
hsharsha
Consider the below code snippet:
#include <iostream>
using namespace::std;
class myclass
{
public:
myclass() {
cout << "constructor" << endl;
}
~myclass() {
cout << "destructor" << endl;
}
void func() {
cout << "func" << endl;
}
};
int main(void) {
myclass *obj = new myclass();
delete obj;
obj = NULL; // obj = (myclass*)0;
obj->func(); //..............must be a crash..... but works fine
return 0;
}
i tried the above code in Microsoft compiler 12.0, 13.0 and g++.
the code doesnt crash and the output will be like
constructor
destructor
func
i really dont know how when obj is null, obj->func() is valid and not
crashing ?????????????/
anybody has an idea about it ??????????
regards,
Harsha.
#include <iostream>
using namespace::std;
class myclass
{
public:
myclass() {
cout << "constructor" << endl;
}
~myclass() {
cout << "destructor" << endl;
}
void func() {
cout << "func" << endl;
}
};
int main(void) {
myclass *obj = new myclass();
delete obj;
obj = NULL; // obj = (myclass*)0;
obj->func(); //..............must be a crash..... but works fine
return 0;
}
i tried the above code in Microsoft compiler 12.0, 13.0 and g++.
the code doesnt crash and the output will be like
constructor
destructor
func
i really dont know how when obj is null, obj->func() is valid and not
crashing ?????????????/
anybody has an idea about it ??????????
regards,
Harsha.