“nullptr“ should be used to denote the null pointer

[复制链接]
发表于 2025-10-21 02:32:54 | 显示全部楼层 |阅读模式



Before C++11, the only way to refer to a null pointer was by using the integer literal
 0, which created ambiguity with regard to whether a pointer or an integer was intended. Even with the
 NULL
 macro, the underlying value is still
 0.
C++11 introduced the keyword
 nullptr, which is unambiguous and should be used systematically.
Noncompliant Code Example

  1. void f(char *c);
  2. void g(int i);
  3. void h()
  4. {
  5.     f(0); // Noncompliant
  6.     f(NULL); // Noncompliant
  7.     g(0); // Compliant, a real integer
  8.     g(NULL); // Noncompliant, NULL should not be used for a real integer
  9. }
复制代码
  1.  
复制代码
Compliant Solution

  1. void f(char *c);
  2. void g(int i);
  3. void h()
  4. {
  5.     f(nullptr); // Compliant
  6.     g(0);  // Compliant, a real integer
  7. }
复制代码
See



  • C++ Core Guidelines ES.47
     - Use nullptr rather than 0 or NULL

For example




免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
继续阅读请点击广告

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

×
登录参与点评抽奖,加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表