_ _ _Below 程序在“delete p”和“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;
}