You don't declare it as such, but hello() is a static function. It doesn't matter what value p holds and the compiler doesn't care. It's going to call hello(). This is a silent optimization.
You can verify that this is the case by replacing hello() with this:
sure, what you said about dereference p to get data member is correct.
but why the value p holds the compiler doesn't care?
as I tried in vc2005,
the first call f(), "p" and "this" has address as 0x003b6ca0.
the second call f(), "p" and "this" has address as 0x00000000.
so the "this" pointer is a temporary??? and can be generated as an arbitrary value???
why even with NULL pointer, we can still call the function correctly?
Because 'this' isn't used in the function in this case.
What you're doing is similar to this:
1 2 3 4 5 6 7 8 9 10
void func(test* foo)
{
cout << "func()";
}
int main()
{
test* t = 0;
func( t ); // works just fine, even though 't' is null because it's never used
}
That's why it's working for you.
However it is not guaranteed to always work (if, for example, 'hello' was a virtual function, your program would explode) so it's ill advised to do such a thing.