nullptr
is a keyword introduced in C++11 that represents a null pointer constant, providing a type-safe way to denote a pointer that does not point to any object or function.
Prior to C++11, the NULL
macro was commonly used to represent null pointers. However, NULL
is defined as 0
or ((void*)0)
, which can lead to ambiguity in function overloading and type safety issues. The introduction of nullptr
resolves these issues by being a distinct type that is specifically designed to represent null pointers.
nullptr
can be used in place of null pointer constants in pointer declarations and assignments, function arguments, and comparisons. It is of type std::nullptr_t
, which can be implicitly converted to any pointer type and can also be used in overload resolution.
NULL
, nullptr
is not an integer, which eliminates ambiguity when passing null pointers to overloaded functions.nullptr
ensures that the appropriate function signature is chosen based on the pointer type.nullptr
is fully compatible with existing pointer types, meaning it can be assigned to any pointer without requiring explicit casting.#include <iostream>
void process(int* ptr) {
std::cout << "Processing an integer pointer.\n";
}
void process(char* ptr) {
std::cout << "Processing a character pointer.\n";
}
int main() {
int* intPtr = nullptr; // Initializing an integer pointer to nullptr
char* charPtr = nullptr; // Initializing a character pointer to nullptr
process(intPtr); // Calls process(int*)
process(charPtr); // Calls process(char*)
return 0;
}
#include <iostream>
void foo(int) {
std::cout << "Called foo with int.\n";
}
void foo(char*) {
std::cout << "Called foo with char*.\n";
}
int main() {
foo(0); // Calls foo(int), may be misleading
foo(nullptr); // Calls foo(char*), clear and type-safe
return 0;
}
NULL
due to habit, potentially leading to confusion, especially in overload situations. Using nullptr
is encouraged for clarity.NULL
or 0
, be cautious, as passing nullptr
may require explicit casting in some contexts.nullptr
: Be aware that comparing pointers with nullptr
will not cause any ambiguity, but comparing with NULL
might lead to unintended results if the context is not clear.nullptr
will always resolve to the pointer overload, which can be a source of confusion if not understood.nullptr
is a type-safe null pointer constant introduced in C++11, enhancing clarity and preventing ambiguity in pointer-related operations.