“nullptr“ should be used to denote the null pointer

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

cpp 复制代码
void f(char *c);
void g(int i);
void h()
{
    f(0); // Noncompliant
    f(NULL); // Noncompliant
    g(0); // Compliant, a real integer
    g(NULL); // Noncompliant, NULL should not be used for a real integer
}
复制代码
 

Compliant Solution

cpp 复制代码
void f(char *c);
void g(int i);
void h()
{
    f(nullptr); // Compliant
    g(0);  // Compliant, a real integer
}

See

For example

相关推荐
DokiDoki之父1 分钟前
边写软件边学kotlin(二)——语法推进
开发语言·微信·kotlin
王老师青少年编程5 分钟前
2020年信奥赛C++提高组csp-s初赛真题及答案解析(选择题6-10)
c++·题解·真题·初赛·信奥赛·csp-s·提高组
GIS阵地9 分钟前
如何统计QGIS里栅格图层的面积呢
c++·qgis·开源gis·pyqgis
汉克老师8 小时前
GESP2024年6月认证C++二级( 第一部分选择题(9-15))
c++·循环结构·分支结构·gesp二级·gesp2级·求余数
清水白石0089 小时前
突破并行瓶颈:Python 多进程开销全解析与 IPC 优化实战
开发语言·网络·python
王老师青少年编程10 小时前
csp信奥赛c++高频考点假期集训(分模块进阶)
数据结构·c++·算法·csp·高频考点·信奥赛·集训
百锦再10 小时前
Java之Volatile 关键字全方位解析:从底层原理到最佳实践
java·开发语言·spring boot·struts·kafka·tomcat·maven
daad77710 小时前
rcu 内核线程
java·开发语言
xzjiang_36510 小时前
检查是否安装了MinGW 编译器
开发语言·qt·visual studio code
王老师青少年编程11 小时前
2020年信奥赛C++提高组csp-s初赛真题及答案解析(选择题1-5)
c++·题解·真题·初赛·信奥赛·csp-s·提高组