From 0 to NULL
In the C ++ world literal 0 is used to indicate null pointers, 0 can be used as a pointer for all types of literals. In order to more clearly introduces a semantic NULL
macro definitions:
#undef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
This shows in C ++, NULL
is replaced (in some implementations of 0 NULL
may be defined as 0L), this is because C ++ does not allow void
pointer is implicitly converted to other types of pointers, but allows 0 as each pointer type literals constant.
. While doing so can be used, but there are some minor drawbacks: 1 each use NULL
before the need to ensure that the introduction of the macro definition, the general introduction of <cstddef>
header files; 2 Some scenes will be in trouble. :
void func(char *str) {
printf("char *");
}
void func(int n) {
printf("int");
}
int main() {
func(NULL);
return 0;
}
Here I wanted to call the func(char *)
version of the function, but in fact it is called func(int)
version. In particular, some achieve NULL
defined as 0L, where the compiler will report an ambiguous error.
From NULL to nullptr
For this purpose, C ++ 11 introduces a new keyword nullptr
, specially since before the new name is to use compatible NULL
code . nullptr
It is typed, and the type definition from nullptr
itself:
typedef decltype(nullptr) nullptr_t;
nullptr
The perfect solution to two problems above.