c++中空指针也是可以调用成员函数的,但是需要注意有没有用到this指针;
#include<iostream>
using namespace std;
#include<string>
//空指针调用成员函数
class person
{
public:
void showclassName()
{
cout << "this is person" << endl;
}
void showPersonAge()
{
//报错的原因是因为传入的指针是为空
if(this == NULL)
{
return;
}
cout << "personage is =" << m_age << endl;
}
int m_age;
};
void test()
{
person *p = NULL;
p->showclassName();
p->showPersonAge();
}
int main()
{
test();
cout << "程序运行结束" << endl;
system("pause");
return 0;
}