空指针可以访问成员函数:
#include<iostream>
#include<string>
using namespace std;
class person {
public:
void personShow()
{
cout << "调用personShow成员函数" << endl;
}
void ageShow()
{
cout << "age: " << m_age << endl;
}
int m_age;
};
void test01()
{
person *p = NULL;
//p->ageShow();
p->personShow();
}
int main()
{
test01();
}
空指针不可以访问成员变量:
示例代码
#include<iostream>
#include<string>
using namespace std;
class person {
public:
void personShow()
{
cout << "调用personShow成员函数" << endl;
}
void ageShow()
{
cout << "age: " << m_age << endl;
}
int m_age;
};
void test01()
{
person *p = NULL;
p->ageShow();
}
int main()
{
test01();
}