//空指针可以调用成员函数,但是要注意this指针
#include<iostream>
using namespace std;
class person {
public:
void ShowClassName() {
cout << "this is class person" << endl;
}
void ShowPersonAge() {
if (this == NULL) {
return;
} //m_age前面有一个this,而传入空指针
cout << "the age is " << m_age << endl;//就意味着没有实体
//的所属对象,所以会报错
}
int m_age;
};
void test03() {
person* p = NULL;
p->ShowClassName();
p->ShowPersonAge();
}
int main() {
test03();
return 0;
}