“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

相关推荐
Elias不吃糖4 小时前
Java Lambda 表达式
java·开发语言·学习
guygg884 小时前
一级倒立摆MATLAB仿真程序
开发语言·matlab
暮色_年华5 小时前
随想 2:对比 linux内核侵入式链表和 STL 非侵入链表
linux·c++·链表
情缘晓梦.5 小时前
C语言指针进阶
java·开发语言·算法
世转神风-5 小时前
qt-字符串版本与数值版本互转
开发语言·qt
极客代码5 小时前
深入解析C语言中的函数指针:原理、规则与实践
c语言·开发语言·指针·状态机·函数·函数指针
w-w0w-w6 小时前
C++模板参数与特化全解析
开发语言·c++
不绝1916 小时前
C#核心:继承
开发语言·c#
大锦终7 小时前
递归回溯综合练习
c++·算法·深度优先