Let us understand the need of nullptr with help of an example.

#include <iostream>
using namespace std;

class base {
void display(int num)
cout << “Integer display: “ << num << endl;
void display(char* ptr)
cout << “ptr display: “ << ptr << endl;
int main()
    base b;
    return 0;

The above code will not compiled. Below error will come:

“In function int main():
    15:15: error: call of overloaded display(NULL) is ambiguous       
15:15: note: candidates are:
    5:10: note: void base::display(int)
    8:10: note: void base::display(char)

NULL is basically (void *)0 and it can be implicitly convertible to integral types. So, when we pass NULL in the argument, then it can be converted in to any integral type as well. Hence the ambiguity comes.

In order to remove this ambiguity, C++ 11 standards came up with nullptr keyword.

nullptr is like NULL but cannot be converted in to any intgral types. so when we call display function like below:


The code will be compiled successfully using -std=c++11 flag. Output of the above program will be:

ptr display: 

Important Points:
Like NULL, nullptr is implicitly convertible or comparable to any pointer type.

Unlike NULL, nullptr is not implicitly convertible or comparable to any integer variable except for bool.

nullptr is of type std::nullptr_t and cannot take the address of it.

Related posts