(1)空指针同样能够调用成员函数,只不过要注意this指针的指向
1:在没有成员变量的函数中可以调用
using namespace std;
class person{
public:
void test01(){
cout<<"this is test01"<<endl;
}
void test02(){
cout<<"age="<<M_A<<endl;
}
int M_A;
};
void test03(){
person *p=NULL;
p->test01();
//p->test02();
}
int main()
{
test03();
}
这种情况无法调用
using namespace std;
class person{
public:
void test01(){
cout<<"this is test01"<<endl;
}
void test02(){
cout<<"age="<<M_A<<endl;
}
int M_A;
};
void test03(){
person *p=NULL;
//p->test01();
p->test02();
}
int main()
{
test03();
}
(2)在调用某个对象的成员变量时此时的成员变量实际上有this指针指向
cout<<"age="<M_A<<endl;