Run the following code. What is the result?
Class
{
PRIVATE:
Int m_value;
Public:
A (INT value)
{
M_value = value;
}
Void print1 ()
{
Printf ("Hello World ");
}
Void print2 ()
{
Printf ("% d", m_value );
}
};
Int _ tmain (INT argc, _ tchar * argv [])
{
A * pA = NULL;
Pa-> print1 ();
Pa-> print2 ();
Return 0;
}
Analysis:
The answer is that the print1 call is normal and the Hello world is printed, but the program crashes when it runs to print2.
When you call print1, you do not need the PA address because the function address of print1 is fixed. The compiler will pass in a this pointer to print1, Which is null, but this pointer is not used in print1. As long as the program is running without accessing the memory that shouldn't be accessed, no error occurs, so it runs normally.
When running print2, this pointer is required to obtain the m_value value. The program crashes because this pointer is null.