神奇的hello world

来源:互联网 发布:淘宝店铺违规怎么举报 编辑:程序博客网 时间:2024/06/04 21:04
#include "stdio.h"
 
class CTest
{
public:
    void print(const char* const pszMsg)
    {
       printf("%s\n",pszMsg);
    }
};
 
int main(void)
{
    CTest* pTest = NULL;
    pTest->print("hello world");
 
    return 0;

}



C++中类成员对象函数会被翻译成void print(const char* const pszMsg, CTest* this),
如果在里面调用对象数据成员(假如这里有个private int 的m_nTemp),
执行printf("m_nTemp:%d\n",m_nTemp);实际上等价于printf("m_nTemp:%d\n",this->m_nTemp)。
这时才会报空指针异常


而如果调用static数据成员或static函数,则根本不会调用this,即使是空指针也不会有影响。

具体就不研究了 = = 。。