Get the App
SLTechnology News&Howtos  ›  Development  › 

Can the null pointer of C++ use nullptr instead of NULL?

Shulou Source: shulou.com Published: 2022-06-01 12:59:30 09月22日 Update

This article mainly introduces "can the null pointer of C++ use nullptr instead of NULL?". In daily operation, I believe that many people have doubts about whether the null pointer of C++ can use nullptr instead of NULL. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question that "can the null pointer of C++ use nullptr instead of NULL?" Next, please follow the editor to study!

First of all, let's learn what a null pointer is. A null pointer is void*.

In order to avoid the appearance of a wild pointer (that is, the pointer does not point to any address), it is best to initialize a pointer as soon as it is declared.

If it is not clear which variable the pointer points to for the time being, you can give NULL, such as:

Int* p = NULL

In addition to NULL, the new standard called nullptr has been introduced to represent a null pointer.

Nullptr is neither an integer type nor a pointer type. The type of nullptr is std::nullptr_t, which can be converted to any pointer type.

Why is nullptr recommended instead of NULL?

This is because in C++, NULL is a constant defined as 0, and there is a problem when a function is overloaded.

For example, when there are two functions:

Void foo (int n)

Void foo (char* s)

Function overloading: C++ allows multiple similar functions of the same name to be declared in the same scope, and the formal parameter lists (number of parameters, type, order) of these functions must be different.

# include using namespace std;void foo (int n) {cout

Tags: Pointers functions clocking types learning compiling two pointing more compilers problems help different practical next number ambiguity code functions parameters Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Linux vpn Huawei MariaDB