C++中空指针也是可以调用成员函数的,但是也要注意有没有用到this指针
如果用到this指针,需要加以判断保证代码的健壮性
//空指针调用成员函数class Person{public:void showClassName(){cout << "this is Person class" << endl;}void showPersonAge(){//报错原因是因为传入的指针是为NULLif (this == NULL){return;}cout << "age = " << this->m_Age << endl;}int m_Age;};void test01(){Person* p = NULL;p->showClassName();p->showPersonAge(); //报错 空指针不能访问成员属性}int main(){test01();system("pause");return 0;}
