#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Person
{
public:
void showClass()
{
cout << "class Name is Person" << endl;
}
void showAge()
{
/* if (this == NULL)
{
return;
}*/
//防止我传的指针是空指针,防止传空指针恶意盗用
//m_Age = 0;
cout << "age = " << this->m_Age << endl;//隐藏的都会有this指针,加不加this都可以
}
//如果成员函数没有用到this指针,那么可以用空指针调用成员函数
//如果成员函数用到了this,那么需要加一个判断,防止这个t程序down掉
int m_Age;
};
void test01()
{
Person * p = NULL;
//p->showClass();
p->showAge();
//this指针指向的是m_Age
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}
肆-拾陆|空指针访问成员函数
最新推荐文章于 2024-06-15 16:46:40 发布