Definitely, in conditions the comparison of pointer to value does not always have to be explicit:
1 2 3 4 5 6 7 8 9 10
|
T * ptr = ...
if ( ptr ) {
// use ptr
}
// and
Base * objp = ...
if ( auto derp = dynamic_cast<Derived*>(objp) ) {
// use derp
}
| |
Why different things? Legacy and clarity. C++ inherits from C. In the beginning someone decided that integer value 0 is used with "empty pointers". However, when the code has many integer 0's the pointer's 0 does not stand out. C has preprocessor macros and C libraries started to define macro NULL. When code shows word
NULL
, it is more obvious that it is not doing usual integer math.
Whoever invented code pages (ASCII table, UTF-8, etc) desided that (unprintable) character
\0
has numeric value 0. Again, clarity; all zeros are zero, but here I have character rather than integer.
As said, modern C++ code should use
nullptr
.