空指针访问成员函数
C++中空指针也是可以调用成员函数的,但是也要注意有没有用到this指针
如果用到this指针,需要加以判断保证代码的健壮性
实例:
#include <iostream>
using namespace std;
class Preson {
public:
void showClassName()
{
cout << "this is Preson class" << endl;
}
void showPersonAge()
{
if (this == NULL) {
return;
}
cout << "age = " << m_Age << endl;
}
int m_Age;
};
void test01()
{
Preson* p = NULL;
p->showClassName();
p->showPersonAge();
}
int main()
{
test01();
system("pause");
return 0;
}