-
如果成员函数没有用到this ,那么空指针可以直接访问
-
如果成员函数用到this 指针,就要注意,要判断是否为空,防止程序崩溃
#include<iostream> using namespace std; class Person { public: void show() { //没有 用到this指针,空指针可以访问函数 cout << "Person show" << endl; } void showAge() { if (this == NULL) { return; } cout << m_Age << endl; } int m_Age; }; void test01() { Person *p = NULL; p->show(); p->showAge(); } int main() { test01(); system("pause"); return 0; }
c++中空指针访问成员函数
最新推荐文章于 2024-06-30 23:07:12 发布