-
如果成員函數沒有用到this ,那么空指針可以直接訪問
-
如果成員函數用到this 指針,就要注意,要判斷是否為空,防止程序崩潰
#include<iostream>using namespace std;class Person{public:void show(){//沒有 用到this指針,空指針可以訪問函數cout << "Person show" << endl;}void showAge(){if (this == NULL){return;}cout << m_Age << endl;}int m_Age;};void test01(){Person *p = NULL;p->show();p->showAge();}int main(){test01();system("pause");return 0;}