To understand why this happens , you should know what is a class, what is an object.
In case of your class A,
if you try to execute the statement sizzeof(A) , then you will realize it will be the sum of size of
data members
This itself mean that , only data members are part of any object and the corresponding functions are not.
Then how does any function accesses the data members if it is not a part of object ?
This is where famous
this pointer comes into picture.
Hence if there is a code like
A *ptr = new A();
ptr->func();
This code will be converted by compiler to
A *ptr = new A();
func(ptr);
Hence your func function will access data members using the passed ptr (i.e.
this pointer )
void func( A* this )
{
printf("Some text");
}
if we chose to replace initialization of the ptr to
NULL then we will be passing a
NULL pointer to function.
A *ptr = NULL;
ptr->func();
Compiler will convert this code to
A *ptr = NULL;
func(ptr);
In func, if you try to access any data member
A::func()
{
printf( "a = %d", a);
}
compiler will convert this code to
func( A* this )
{
printf("a = %d", this->a);
}
Now you can observe that , as we are sending NULL pointer to the function. It has to crash :)