C++中空指针也可以调用成员函数,要注意有没有用到this指针
如果用到this指针,需要加以判断保证代码的健壮性
//空指针调用成员函数
class Person
{
public:
void showClassname()
{
cout << "Person" << endl;
}
void showPersonage()
{
//报错原因:传入的指针为NULL,this没有东西可指
//解决:添加下面代码
if (this == NULL)
{
return;
}
cout << "age = " <<this-> m_age << endl;
}
int m_age;
};
void test01()