##空指针访问成员函数
C++中空指针也是可以调用成员函数的,但是也要注意有没有用到this指针,如果用到this指针,需要加以判断保证代码的健壮性。
若class类中是static静态成员变量/函数则即使Person * p = NULL调用也不会报错,因为static静态成员是和非静态成员放开存储,而且静态成员在计算机的存储中有且只有一份,采用共享的方式存在。
#include<iostream>
#include<algorithm>
using namespace std;
class Person
{
public:
static void aa(int a)
{
age=a;
cout << "ssss"<<age<<endl;
}
static int age;
};
int Person::age=0;
int main()
{
Person *p1=NULL;
p1->aa(10);
}
即使person *p1=NULL;指向空指针也可以成功访问