Possible Duplicate:
Accessing class members on a NULL pointer
A very silly question or may be my conceptual doubt.
#include <iostream>
using namespace std;
class A
{
public:
void print()
{
cout<<"Printing\n";
}
};
int main()
{
A * a = NULL;
a->print();
return 0;
}
The output is: Printing
How is a pointer(which is NULL) able to access member function of class A.
Please explain… may be its just a silly question but I had the impression that a NULL pointer will not access the class’s member function.
Dereferencing a
NULLpointer, as you do in your code, is undefined behavior. One of the possibilities of such undefinedness is that it may just work. But it may crash the next time, or do something totally unexpected.Since you are not using the implicit
thisargument in your member functionprint, seems like suchNULLpointer never needs to actually be dereferenced.