difference between pointers and references
I was reading up on the differences between pointers and references because even though they operate similarly, i.e. through indirection, they have very different mechanics and semantics.
I couldn't find a direct answer searching the forums because I think the keyword null in modern OOP languages (i.e. C#, Java) is implemented differently then the NULL macro in C++, C which is defined as 0.
So, I perused Stack Overflow and I found this post:
Difference between pointer variable and reference variable in C++ - Stack Overflow
And what caught my eye was point number 4 in the answer to the post given by Brian R. Bondy.
"4) Pointer can be assigned NULL directly, whereas reference cannot. If you try hard enough, and you know how, you can make the address of a reference NULL. Likewise, if you try hard enough you can have a reference to a pointer, and then that reference can contain NULL."
Now this confused me a little because I was weaned on OOP languages such as Java and C# and from what I read in books, when an object is created using the new operator, memory is allocated for the object on the heap and a reference is returned and stored in the object variable.
int *p = NULL;
int &r = NULL; <--- compiling error
If this is the case then how come the following code in C# is acceptable as well as Java?
If anyone can provide and explanation, it would ease my mind greatly because I'll be thinking about it all the time if I don't get closure.
Square square = new Square();
square = null;
And for further evidence, according to the MSDN C# reference:
The null keyword is a literal that represents a null reference, one that does not refer to any object.
Source: null (C# Reference)