C++空指针访问成员函数

C++中,空指针也可以访问成员函数,但是要注意有没有用到this指针。

如果用到this指针,需要加以判断保证代码的健壮性。

#include<iostream>
using namespace std;

class WLM
{
public:
    void A()
    {
        cout << "666" << endl;
    }

    void B()
    {
        if(this == NULL)    //保证代码的健壮性
        {
            return;
        }
        cout << m_a << endl;
    }

    int m_a;
};

int _tmain(int argc, _TCHAR* argv[])
{
    WLM * wlm = NULL;
    
    //wlm->A();

    wlm->B();

    system("pause");
    return 0;
}
posted @ 2022-07-29 13:48  萌萌的保镖  阅读(70)  评论(0)    收藏  举报