According to the C++ standard, it's undefined behavior. However, a typical C++ implementation will treat a non-virtual member function
void A::func(int arg);
like the C-style function
void A_func(A* this, int arg);
And just like any pointer argument, it's OK for "this" to be NULL as long as you don't dereference the memory. So, if you write:
#include <iostream>
class A
{
public:
void test();
};
void A::test()
{
std::cout << "This function could have been static." << std::endl;
}
int main()
{
((A*) NULL)->test();
return 0;
}
It will run just fine (at least under MSVC and G++).
However, this gives a segmentation fault:
#include <iostream>
class A
{
public:
void test();
private:
int _n;
};
void A::test()
{
_n = 42;
}
int main()
{
((A*) NULL)->test();
return 0;
}
The if (this == NULL) check is an attempt to prevent a null pointer dereference from crashing the program.
But to conform to the standard, it's better to do the test from the outside.
if (pList != NULL)
{
pList->Insert(value);
}