Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is there no "NULL reference" in C++?
    primarykey
    data
    text
    <p>I was reading the C++ FAQ - "<a href="http://www.parashift.com/c++-faq-lite/refs-vs-ptrs.html" rel="nofollow noreferrer">8.6 - When should I use references, and when should I use pointers?</a>" and in particular this statement:</p> <blockquote> <p>Use references when you can, and pointers when you have to.</p> <p>...</p> <p>The exception to the above is where a function's parameter or return value needs a "sentinel" reference — a reference that does not refer to an object. This is usually best done by returning/taking a pointer, and giving the NULL pointer this special significance (references must always alias objects, not a dereferenced NULL pointer).</p> </blockquote> <p>From what I've seen, the need for a "sentinel" reference is indeed often the reason to use pointers instead of references. What I'm wondering is: why doesn't C++ have a special "NULL value" for references? It seems it would make pointers almost unnecessary, which would solve many problems.</p> <p>So why wasn't it part of the language specification?</p> <p><strong>Edit:</strong></p> <p>I'm not sure my question is clear - I guess I'm not asking litterally about NULL references. Most often I read that in C++ "the reference is the object". And, in most OOP languages, objects can be NULL - Pascal, C#, Java, JavaScript, PHP, etc. in all these you can do <code>someObject = null</code> or <code>someObject := nil</code>. In fact, Pascal also supports pointers but still allows objects to be <code>nil</code>, since it has its use. So why is C++ somehow special and doesn't have a NULL object? Was it just an overlook or an actual decision?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload