_ _ _以下のプログラムは、「deletep」および「p = NULL」の後にMyClass::processingが呼び出されるとクラッシュするはずですが、「MyClass::a」にアクセスしようとするとクラッシュします。なぜそうなのか???
#include <iostream>
using namespace std;
class MyClass
{
public:
int a;
void processing()
{
cout<<"Processing"<<endl;
}
};
int main(void)
{
MyClass* p(new MyClass);
MyClass* q = p;
p->a = 10;
cout<<"p:: "<<p<<" q:: "<<q<<endl;
cout<<"p->a"<<p->a<<"q->a"<<q->a<<endl;
delete p;
p->processing(); // Watch out! p is now dangling!
cout<<"\n\nAfter Deletion::"<<endl;
cout<<"p:: "<<p<<" q:: "<<q<<endl;
cout<<"p->a"<<p->a<<"q->a"<<q->a<<endl;
p = NULL; // p is no longer dangling
cout<<"\n\nAfter Assigning null"<<endl;
p->processing(); // Watch out! p is now dangling!
q->processing(); // Ouch! q is still dangling!
cout<<"p:: "<<p<<" q:: "<<q<<endl;
}