头文件已经正常包含,以下代码在VS IDE上编译和运行结果是
class A{
public:
void test(){printf("test A");}
};
int main()
{
A *pA = NULL;
pA->test();
return 0;
} #include<iostream>
using namespace std;
class A{
public:
void test()
{ printf("test A"); }
};
int main(){
A* pA = NULL;
pA->test();
return 0;
}
其实这个是可以正常运行的
pA->test();这语句的意图是:调用对象 pA 的 test 成员函数。如果这句话在Java或Python等动态绑定的语言之中,编译器生成的代码大概是:
Class A
{
public:
A(int a){this->a = a;}
void GetA1(){cout << "void GetA1()" << endl;}
void GetA2(){cout << this->a << endl;}
public:
int a;
}
A* p = NULL;
p->GetA1();//ok
p->GetA2();//err 1 如果成员函数中没有用到this指针,可以用空指针调用该成员函数
2 如果成员函数中用到了this,那么需要对这个this加判断语句,防止空指针解引用