pointer to object

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  #include<iostream>

using namespace std;

class TestNULL{
public:
	void print()
	{
		cout<<"\n In Print Function";
	}
};


int main()
{
	TestNULL * ptr = NULL;
	ptr->print();           //Why it didn't break here???
	return 0;
}


Output of above code is In Print Function

ptr->print(); why compiler didnt complain at this line??
The compiler does not have code that tries to access the memory at address 0. In fact the code that calls the function

ptr->print();

is equivalent to

print( this );

that in turn is equivalent to

print( NULL );

As the argument of the function is not used there is no any problem.
yes i got it... thanks vlad from moscow
Topic archived. No new replies allowed.