#include<iostream>
#include<string>
using namespace std;
class Person {
public:
void showClassName() {
cout << "this is person class" << endl;
}
void showPersonAge() {
//报错原因:你传入的指针为空
if (this == NULL) {
return;//可以用这种判断来防止空指针的问题
}
cout << m_Age << endl;
}
int m_Age;
};
void test01() {
Person *p = NULL;
p->showClassName();//单独调用这个的时候没有问题
p->showPersonAge();//这个不可以,因为你没有建立对象,所以非静态成员函数内的this不知道要指向哪个对象
}
int main() {
test01();
return 0;
}
空指针访问成员函数
最新推荐文章于 2023-09-07 10:30:10 发布