首页 > 编程语言 > 详细

c++之空指针访问成员函数

时间:2019-12-25 14:10:46      阅读:89      评论:0      收藏:0      [点我收藏+]
#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++之空指针访问成员函数

原文:https://www.cnblogs.com/xiximayou/p/12096066.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!