Dereferencing a NULL
pointer means that you have undefined behavior.
However in this case on most implementations you will end up in correct function with this
being NULL
, the normal implementation of calling a non-virtual method is to set the hidden parameter this
as being the pointer to the object (in this case NULL
) and then just call the function. If you don't access member variables or call virtual methods you should be fine in most implementations.
Since you can't access members or call virtual functions or in any other way do anything useful with this
pointer you're pretty close to the scenario of a static
method and I'll suggest one use that instead if the this
pointer is not used.
For the corner case where you want to be able to do nothing else with the this
pointer than check for NULL
ness you could still use a static
method and explicitely pass the pointer to the object:
static void func(foo* diz) {
cout << "In func" << endl;
if( diz != NULL ) {
diz->actual_work_on_diz();
}
}