#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;
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,即使是空指针也不会有影响。
具体就不研究了 = = 。。