Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) nullptr

 

nullptr is a keyword that has different meanings, depending on the standard used:

 

 

 

 

 

C++98 nullptr in the C++98 standard

 

nullptr is not supported in C++98.

 

 

 

 

 

C++11 nullptr in the C++11 standard

 

 

nullptr is a keyword to indicate an unitialized pointer.

 

#include <cassert>

int main()
{
  //Create a new p
  int * p = new int(3);
  assert(*p == 3);

  //Get rid of the current p
  delete p;
  p = nullptr;

  //Create a new p
  p = new int(4);
  assert(*p == 4);
}

 

 

 

 

 

Advice

 

 

 

 

 

 

References

 

  1. GCC page about C++0x support
  2. Scott Meyers. C++ And Beyond 2012 session: 'Initial thoughts on Effective C++11'. 2012. 'Prefer nullptr to NULL and 0'

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict