空指针访问成员函数
若成员函数没有用到 this ,那么空指针可以直接访问
如果成员函数用到 this 指针,就要注意, 可以加if判断,如果 this 为空就 return
class Person0007 {
public:
void show() {
cout << "Person show." << endl;
}
void showAge() {
if (this == NULL) {
return;
}
cout << m_Age << endl;//NULL->m_Age;
}
int m_Age;
};
void test000007() {
Person0007* p = NULL;
p->show();
p->showAge();//无法运行
}