#include<iostream> using namespace std; class Person { public: int age; void showClass() { cout << "这是Person类" << endl; } void showAge() { //解决方法,如果是空就直接返回 if (this == NULL) { return; } cout << "年龄是:" << this->age << endl; } }; void test() { Person* p = NULL; //p->showAge();会报错,因为空指针访问成员属性不可行 p->showClass(); } int main() { test(); system("pause"); return 0; }
c++之空指针访问成员函数
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
深度解读《深度探索C++对象模型》之C++虚函数实现分析(一)
本系列深入分析编译器对于C++虚函数的底层实现,最后分析C++在多态的情况下的性能是否有受影响,多态究竟有多大的性能损失。
虚函数 虚函数表 C++对象模型 C++成员函数 C++性能分析 -
C++里空指针访问成员函数#c++指针
-
空指针访问成员函数
空指针访问成员函数C++中空指针也是可以调用成员函数的,但是也要注意有没有用到this指针如
c++ 空指针 推广微信公众号 成员函数 this指针 -
C++成员变量指针和成员函数指针
深度探索C++对象模型这本书还有提到C++类的成员变量指针和成员函数指针,虽然在实际开发中用的不多,但是还是
c++ #include 偏移量 静态成员 -
静态成员函数访问非静态数据成员【C++】
静态成员函数访问非静态数据成员静态数据成员静态成员函数静态数据成员定义格式:
面向对象编程 c++ 类 静态数据成员 静态成员函数