在单例类的静态成员函数中,我没有使用 getInstance(),而是直接使用了静态指针成员变量。
我没有对它进行空检查,它在运行时为空,因此是空指针异常。
PC lint 没有通知我这一点。通常它会通知我作为 Prio 2 警告:可能使用空指针。
为什么没有通知我?
class stClass
{
int m_value;
static stClass *s_instance;
stClass(int v = 0)
{
m_value = v;
}
public:
int get_value()
{
return m_value;
}
void set_value(int v)
{
m_value = v;
}
static stClass *getInstance()
{
if (!s_instance)
s_instance = new stClass;
return s_instance;
}
static void intentFunction()
{
printf("%d", s_instance->getValue()); // Null pointer exception here...
}
};