“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

相关推荐
小小龙学IT5 分钟前
现代 C++ 内存管理与资源安全深度解析:从 RAII 到所有权模型
c++·安全
SomeB1oody10 分钟前
【RustyML入门】5.3. 聚类指标
开发语言·后端·机器学习·rust·教程
欧特克_Glodon14 分钟前
OpenCV计算机视觉开发入门与实践<十三>:图像转换之灰度图、二值图
c++·人工智能·opencv·计算机视觉
OPEN-F18 分钟前
Python进阶教程:Web开发入门(Flask)
开发语言·python
神仙别闹21 分钟前
基于C++ MFC实现动态分区分配存储管理
开发语言·c++·mfc
skr爱码士24 分钟前
03_第一个Qt项目:Hello World 的完整拆解
c++·qt·客户端
for_ever_love__27 分钟前
python基础语法学习: 类型注解
开发语言·python·学习
caimouse32 分钟前
ReactOS 图形系统分析(46):DC 工具 — dcutil.c
c语言·开发语言
欧特克_Glodon35 分钟前
OpenCV计算机视觉开发入门与实践<十二>:XML 和 YAML 文件读写
xml·c++·opencv·计算机视觉
ryanuo743 分钟前
Shadcn/ui × Qt 6/QML:一次从 Web UI 到桌面 UI 的组件化实践
c++·qt·ui·shadcn