Null object: Difference between revisions

no edit summary
(→‎{{header|PureBasic}}: Added PureBasic)
No edit summary
Line 93:
 
=={{header|C++}}==
In C++'s accessnon-pointer totypes 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, isor (as in C) by way of a macro (NULL) which simply evaluatesexpands to 0.
<lang cpp>#include <iostream>
#include <cstdlib>
if (object == NULL0) {
std::cout << "object is null";
}</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#}}==
As with Java, any reference type may be null, and testing for nullity uses ordinary boolean operators.
Anonymous user