NULL vs nullptr

Hi, what is the difference between NULL vs nullptr?

Thank you
Best Regards
Marco

In MSVC, for C++ NULL is just defined as the integer constant 0:

#ifdef __cplusplus
    #define NULL 0
#else

Whereas nullptr is a keyword that was added in C++ 11 (technically it’s a prvalue of type std::nullptr_t, but it’s easier to think of it as a built-in type).

There are a number of reasons why nullptr is preferred over NULL, with one of the main reasons being you can’t accidently set an integer value to nullptr like you can with NULL e.g.:

int var = NULL; // OK, equivalent to int var = 0
int var = nullptr; // Compiler error - var must be an int* type
3 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms