Talk:Null object: Difference between revisions

From Rosetta Code
Content added Content deleted
(some examples are incorrect)
 
(reword at most)
Line 6: Line 6:


I don't know Ada, but I suspect the Ada example to be incorrect as well. --[[User:Ce|Ce]] 12:53, 5 August 2008 (UTC)
I don't know Ada, but I suspect the Ada example to be incorrect as well. --[[User:Ce|Ce]] 12:53, 5 August 2008 (UTC)

I think that insofar as this is a problem, we should fix it by rewording the task. Languages may differ on whether they have null pointers or pointers to null, but that doesn't affect the patterns of usage. --[[User:Kevin Reid|Kevin Reid]] 22:42, 5 August 2008 (UTC)

Revision as of 22:42, 5 August 2008

The C anc C++ examples don't really fit the task description (and it's not possible to do so, because C and C++ don't know a generic null object). What you can have is a null pointer, which is a pointer which doesn't point to any object (and that is indeed what NULL is for). The closest to a null object in C++ would be an empty boost::optional from the boost library.

Note that comparison to NULL may compile for non-pointer objects, but will in general not have the desired semantics: If NULL is defined as 0 (possible in C, more or less required in C++), then comparing any numeric object (or any other object with conversion from or to int) will actually compare the value of the object with zero.

Strictly speaking the Java example is incorrect as well (it doesn't test for a null object, but for a null reference), but since in Java objects are always accessed trough references, this distinction is probably mostly academic (note, however, that built-in fundamental types are not objects in the Java sense, and the null test isn't possible for them).

I don't know Ada, but I suspect the Ada example to be incorrect as well. --Ce 12:53, 5 August 2008 (UTC)

I think that insofar as this is a problem, we should fix it by rewording the task. Languages may differ on whether they have null pointers or pointers to null, but that doesn't affect the patterns of usage. --Kevin Reid 22:42, 5 August 2008 (UTC)