Back to articles
Null Pointer

Null Pointer

via Dev.toHaris

The main reason to use nullptr over NULL or 0 is that the nullptr is of type std::nullptr_t this is very useful in case of function overloading. Consider this example: void func(int); void func(bool); void func(void*); Now if we call the functions by the following arguments: func(0) -> This will call func(int) not func(void*) func(NULL) -> This will also call func(int) So why does NULL also calls func(int)? This is because NULL's possible implementation is: #define NULL 0 Or depending on your compiler in modern C++ version it can be #define NULL nullptr so just to be on the safe side always use 'nullptr'. In case of pointer type (int*) the compiler correctly deduces the integer 0 to be a null pointer but like above in case of function overloading where a function overload with integer already exists will always overshadow the pointer argument overload. Readability And Clarity Another important thing that nullptr does well is readability along with auto keyword, consider this example: a

Continue reading on Dev.to

Opens in a new tab

Read Full Article
2 views

Related Articles