Null object: Difference between revisions
Content added Content deleted
(→{{header|PureBasic}}: Added PureBasic) |
No edit summary |
||
Line 93: | Line 93: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
C++ |
In C++ non-pointer types do not support null. (C++ provides value semantics rather than reference semantics). When using pointers C++ permits checking for null by comparing the pointer to a literal of 0, or (as in C) by way of a macro (NULL) which simply expands to 0. |
||
<lang cpp>#include <iostream> |
<lang cpp>#include <iostream> |
||
#include <cstdlib> |
#include <cstdlib> |
||
if (object == |
if (object == 0) { |
||
std::cout << "object is null"; |
std::cout << "object is null"; |
||
}</lang> |
}</lang> |
||
boost::optional is available for cases where the programmer wishes to pass by value, but still support a null value. |
|||
<lang cpp> |
|||
#include <boost/optional.hpp> |
|||
#include <iostream> |
|||
boost::optional<int> maybeInt() |
|||
int main() |
|||
{ |
|||
boost::optional<int> maybe = maybeInt(); |
|||
if(!maybe) |
|||
std::cout << "object is null\n"; |
|||
} |
|||
</lang> |
|||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
As with Java, any reference type may be null, and testing for nullity uses ordinary boolean operators. |
As with Java, any reference type may be null, and testing for nullity uses ordinary boolean operators. |