In C++, <span>nullptr</span> is a keyword introduced in C++11 to represent a null pointer, which is a type-safe and semantically clear null pointer constant specifically used to replace the traditional <span>NULL</span> macro or the integer <span>0</span> to indicate that a pointer does not point to any object.
π― 1. What is <span>nullptr</span>?
β Definition:
<span>nullptr</span> is a keyword introduced in C++11 that represents a pointer value that βdoes not point to any objectβ, its type is <span>std::nullptr_t</span>, which can be implicitly converted to any pointer type (such as <span>int*</span>, <span>char*</span>, class pointers, etc.), but cannot be converted to integer types.
β
2. Why use <span>nullptr</span>? – Solving the issues with traditional <span>NULL</span> and <span>0</span>.
β Issues with traditional methods:
In C++98 / C++03, we typically represented a βnull pointerβ in the following ways:
-
Using the integer
<span>0</span>:int *p = 0; // Indicates p does not point to any address
-
Problem:
<span>0</span>is essentially an integer constant, not a pointer type. Although it can be implicitly converted to a pointer, the semantics are unclear, which can lead to function overload ambiguities.
Using the <span>NULL</span> macro:
int *p = NULL; // Traditional way, many people write this
-
Problem:
<span>NULL</span>is usually a macro, defined as<span>0</span>or<span>(void*)0</span>in many libraries, and is not a true pointer type, which also presents type safety and overload ambiguity issues.
β
Purpose of introducing <span>nullptr</span> in C++11:
-
To provide a type-safe and semantically clear keyword specifically representing a null pointer
-
To avoid confusion with the integer
<span>0</span> -
To resolve the ambiguity issues in function overloading with
<span>0</span>/<span>NULL</span> -
Its type is
<span>std::nullptr_t</span>, which can only be assigned to pointer types and cannot be assigned to integers
β 3. π§ Basic usage examples
π§© Example 1: Defining a null pointer
#include <iostream>
using namespace std;
int main() {
int *p1 = nullptr; // Recommended: C++11 null pointer
int *p2 = 0; // Not recommended, traditional way, type is int*
int *p3 = NULL; // Not recommended, usually defined as 0 or (void*)0
cout << "Is p1 a null pointer? " << (p1 == nullptr) << endl; // Outputs 1 (true)
return 0;
}
π Note:
-
<span>p1</span>is a null pointer initialized using<span>nullptr</span>, which is type-safe and semantically clear. -
<span>p2</span>and<span>p3</span>can also represent null pointers, but they are essentially<span>int</span>or macros, which are not type-safe enough.
β
4. π Advantages of <span>nullptr</span> (compared to <span>0</span> and <span>NULL</span>)
|
Feature |
|
|
|
|---|---|---|---|
|
Type |
|
|
Usually |
|
Semantic clarity |
β Clearly indicates a null pointer |
β Just an integer 0 |
β Easily misunderstood, could be 0 or void* |
|
Type safety |
β Can only be assigned to pointer types |
β May be misused in function overloading |
β May lead to function overload call ambiguities |
|
Function overload distinction |
β Can correctly distinguish between pointer and integer overloads |
β Will prioritize matching the int version |
β May call the wrong overload |
|
C++ version |
Since C++11 |
Since C++98 |
C++98 common, but not a standard keyword |
β
5. π Advantages of <span>nullptr</span> in function overloading (key differences!)
π§© Example 2: Issues with <span>0</span> / <span>NULL</span> in function overloading vs the solution with <span>nullptr</span>.
#include <iostream>
using namespace std;
// Overloaded function 1: accepts int
void func(int) {
cout << "func(int)" << endl;
}
// Overloaded function 2: accepts pointer (e.g., int*)
void func(int*) {
cout << "func(int*)" << endl;
}
int main() {
func(0); // Calls func(int), because 0 is an integer
// func(NULL); // Depends on the definition of NULL, could be func(int)!
func(nullptr); // β
Clearly calls func(int*)
return 0;
}
π Possible output:
func(int) // func(0)
func(int*) // func(nullptr)
β If you use
<span>func(0)</span>or<span>func(NULL)</span>, the compiler may incorrectly call<span>func(int)</span>, because<span>0</span>or<span>NULL</span>are essentially integers;β However,
<span>func(nullptr)</span>will definitely call the pointer version of the function, because<span>nullptr</span>is of pointer type, not an integer!
β
6. <span>nullptr</span>‘s type
-
<span>nullptr</span>‘s type is<span>std::nullptr_t</span>(defined in<span><cstddef></span>, but generally does not need to be included manually) -
It can be implicitly converted to any pointer type (such as
<span>int*</span>,<span>char*</span>, class pointers, etc.) -
But cannot be converted to integer types, ensuring type safety.
β
7. Summary: <span>nullptr</span><span>'s features overview</span>
|
Feature |
Description |
|---|---|
|
Keyword |
|
|
Meaning |
Represents a pointer that does not point to any object, i.e., a null pointer |
|
Type |
|
|
Recommended usage |
To initialize pointers as null, to pass null pointers as function arguments, to replace |
|
Advantages |
Type safety, semantic clarity, avoids function overload ambiguities |
|
Difference from 0 / NULL |
|
|
Applicable scenarios |
All situations requiring a βnull pointerβ representation, especially in modern C++ development should prioritize its use |
β 8. Best practice recommendations
|
Scenario |
Recommended practice |
|---|---|
|
Defining a null pointer |
β
Use |
|
Passing a null pointer as a function argument |
β
Pass |
|
Function overloading involving pointers and integers |
β
Use |
|
Replacing the NULL macro |
β
Always use |
|
Using with smart pointers (e.g., |
β
Use |
β 9. Frequently Asked Questions
Q1:<span>nullptr</span> and <span>NULL</span> what is the difference?
|
Comparison item |
|
|
|---|---|---|
|
Essence |
Keyword, type is |
Usually a macro, defined as |
|
Type safety |
β Is a pointer type, will not be confused with integers |
β May be an integer, leading to incorrect function overload calls |
|
Recommendation level |
β Recommended to always use since C++11 |
β Not recommended for use in new code |
Q2: Can I assign <span>nullptr</span> to an integer?
β No! <span>nullptr</span> is a pointer type, and cannot be implicitly converted to an integer, which is an important aspect of its type safety.
int x = nullptr; // β Compilation error
Q3:<span>nullptr</span> can be used for which pointer types?
β Can be used for any pointer type, such as:
int *p1 = nullptr;
double *p2 = nullptr;
string *p3 = nullptr;
class MyClass; MyClass *p4 = nullptr;