nullptr solve the problem

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 NULLmacro definitions:

#undef NULL
#ifdef __cplusplus
    #define NULL 0
#else
    #define NULL ((void *)0)
#endif

This shows in C ++, NULLis replaced (in some implementations of 0 NULLmay be defined as 0L), this is because C ++ does not allow voidpointer 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 NULLbefore 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 NULLdefined 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 NULLcode . nullptrIt is typed, and the type definition from nullptritself:

typedef decltype(nullptr) nullptr_t;

nullptrThe perfect solution to two problems above.

Guess you like

Origin www.cnblogs.com/HachikoT/p/12164309.html