“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

相关推荐
星空椰8 小时前
Python 面向对象高级:继承与类定义详解
开发语言·python
wunaiqiezixin8 小时前
如何在C++中创建和管理线程
c++
白露与泡影8 小时前
2026大厂Java面试题大全!牛客网最新版
java·开发语言
凯瑟琳.奥古斯特8 小时前
高阶子查询题目精炼
开发语言·数据库·python·职场和发展·数据库开发
雪度娃娃9 小时前
转向现代C++——在意为改写的函数添加 override
开发语言·c++
王老师青少年编程9 小时前
csp信奥赛C++高频考点专项训练之前缀和&差分 --【一维差分】:[NOIP 2018 提高组] 铺设道路
c++·前缀和·差分·csp·高频考点·信奥赛·铺设道路
星马梦缘9 小时前
aaaaa
数据结构·c++·算法
喵星人工作室10 小时前
C++火影忍者1.1.2
开发语言·c++
basketball61610 小时前
C++ 中的 ptrdiff_t 详解
开发语言·c++
wunaiqiezixin10 小时前
互斥锁与自旋锁的区别
c++