According to the C ++ standard, this behavior is undefined. However, a typical C ++ implementation will consider a non-virtual member function
void A::func(int arg);
as a function of C-style
void A_func(A* this, int arg);
And just like any pointer argument, there must be NULL for "this" if you are not looking for 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 work very well (at least under MSVC and g ++).
However, this gives a segmentation error:
Check if (this == NULL) - an attempt to prevent dereferencing of a null pointer when a program crashes.
But to comply with the standard, it is better to do the test outside.
if (pList != NULL) { pList->Insert(value); }
dan04
source share