Qt:NULL与nullptr的区别(手写nullptr)

server/2024/9/23 4:54:18/

前言

发现还是有人不知道NULLnullptr的区别,故写此文章。

正文

对于NULL

先看NULL的源码
在这里插入图片描述

我们可以看出这段代码是一个典型的预处理器宏定义块,用于处理 NULL 宏的定义。
先看开头

#if defined (_STDDEF_H) || defined (__need_NULL)
  • 这行代码检查是否已经定义了 _STDDEF_H__need_NULL
    • _STDDEF_H 通常是在包含标准库头文件 <stddef.h> 时定义的宏。
    • __need_NULL 是某些情况下(例如,部分标准头文件仅需要 NULL 定义)会定义的宏。

如果其中一个宏已定义,则代码块会执行。


#undef NULL		/* in case <stdio.h> has defined it. */
  • #undef NULL 取消之前可能在其他头文件(例如 <stdio.h>)中定义过的 NULL,确保后续的定义不会发生冲突。

#if defined(__GNUG__) && __GNUG__ >= 3
#define NULL __null
  • 这一部分是针对 GNU C++ 编译器(G++)版本 3 及以上的特定定义。
    • __GNUG__ 是 G++ 编译器的特定宏,用于检测当前编译器是否为 G++。
    • __null 是 G++ 3.x 及以上版本中的内部表示,用于专门处理空指针的情况。

如果符合条件(G++ 3.x 及以上版本),NULL 被定义为 __null


#else   /* G++ */
#ifndef __cplusplus
#define NULL ((void *)0)
  • 如果当前编译环境不是 G++ 3.x 或更新版本,且不是 C++,那么 NULL 被定义为 ((void *)0)
    • 在 C 语言中,NULL 通常定义为 (void *)0,表示一个空指针。

#else   /* C++ */
#ifndef _WIN64
#define NULL 0
  • 进入了 C++ 的条件分支。
    • 如果正在编译 C++ 代码,且不是 64 位 Windows 环境(未定义 _WIN64),则 NULL 定义为 0
    • 在 C++ 中,NULL 通常直接定义为 0,因为 C++ 允许整数 0 被隐式转换为指针类型。

#else
#define NULL 0LL
#endif  /* W64 */
#endif  /* C++ */
  • 如果是在 64 位 Windows 环境中,NULL 定义为 0LL,表示一个 64 位的长整数常量(long long)。
    • 在 64 位环境中,使用 0LL 以保证 NULL 的大小与指针的大小一致。

#endif  /* G++ */
#endif  /* NULL not defined and <stddef.h> or need NULL.  */
  • 关闭之前的条件编译语句。
  • #endif 对应前面的 #if,表示条件编译的结束。

#undef	__need_NULL
  • 这行代码取消定义 __need_NULL,确保之后不会再次使用这个宏。

总结

  • 在 GNU C++ 3.x 及以上版本中,NULL 定义为 __null
  • 在 C 语言中,NULL 定义为 (void *)0,表示空指针。
  • 在 C++ 中,NULL 定义为 0,因为 0 可以隐式转换为指针类型。
  • 在 64 位 Windows 环境下,为了确保指针和 NULL 的大小一致,定义为 0LL
  • 由以上我们可知NULL既可以表示0又可以表示(void*)0,这就说明它有二义性。

测试

int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);int* ptr = NULL;int b = 0;int* ptr2 = nullptr;QString* ptr3 = nullptr;if (ptr == 0) {qDebug()<<"ptr is null"<<ptr;}if (b == NULL) {qDebug()<<"b is null"<<b;}if (ptr2 == 0) {qDebug()<<"ptr2 is nullptr"<<ptr2;}if (!ptr3) {qDebug()<<"ptr3 is nullptr"<<ptr3;}return a.exec();
}
运行结果

在这里插入图片描述

对于nullptr

我并没有找到它的源码(如果有找到的兄弟麻烦评论区告知下呗),但是我知道nullptr是一种新的数据类型,就是专门用来表示的一种数据类型。在源码中我只看到它简略的说明
在这里插入图片描述
nullptr的出现就是为了解决NULL即表示0又表示(void*)0的二义性问题。

手写nullptr

const class nullptr_t
{
public:// 模板类型转换运算符,可以将 nullptr 转换为任意类型的指针template<class T>inline operator T*() const{return 0;  // 返回值为 0,表示空指针}// 模板类型转换运算符,可以将 nullptr 转换为任意类成员指针template<class C, class T>inline operator T C::*() const{return 0;  // 返回值为 0,表示空成员指针}private:// 重载地址取运算符(&),防止获取 nullptr 的地址void operator&() const;} nullptrSelf = {};

使用:

// widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
const class nullptr_t
{
public:// 模板类型转换运算符,可以将 nullptr 转换为任意类型的指针template<class T>inline operator T*() const{return 0;  // 返回值为 0,表示空指针}// 模板类型转换运算符,可以将 nullptr 转换为任意类成员指针template<class C, class T>inline operator T C::*() const{return 0;  // 返回值为 0,表示空成员指针}private:// 重载地址取运算符(&),防止获取 nullptr 的地址void operator&() const;} nullptrSelf = {};class Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();void test(int* a);private:void* memberPtr;
};
#endif // WIDGET_H// widget.cpp#include "widget.h"
#include "qdebug.h"Widget::Widget(QWidget *parent): QWidget(parent)
{memberPtr = nullptrSelf;qDebug()<<"memberPtr:"<<memberPtr;int* a = nullptrSelf;test(a);}Widget::~Widget()
{
}void Widget::test(int* a)
{if (a == nullptrSelf) {qDebug()<<"a is null "<<a;} else {qDebug()<<"a is not null "<<a;}
}

输出结果

在这里插入图片描述

小结

请指正


http://www.ppmy.cn/server/120634.html

相关文章

Spring AOP - 配置文件方式实现

目录 AOP基础概念 示例1&#xff1a;模拟在com.text包及子包项下所有类名称以ServiceImpl结尾的类的所有方法执行前、执行后、执行正常后返回值、执行过程中出异常的情况 示例2&#xff1a;统计com.text包及子包项下所有类名称以DaoImpl结尾的类的所有方法执行时长情况 AOP基…

利用JAVA写一张纸折叠珠穆拉玛峰高度

public class zhumulama {public static void main(String[] args) {double height 8848860;double zhi 0.1;int count 0;while(zhi < height){zhi*2;//每次折完厚度count;//计数}System.out.println("一共需要折"count"次");System.out.println(&qu…

机器学习查漏补缺(4)

[M] What happens if we accidentally duplicate every data point in your train set or in your test set? Train set duplication: Duplicating every data point in the training set will effectively double the importance of each sample, but it won’t introduce ne…

laravel public 目录获取

在Laravel框架中&#xff0c;public目录是用来存放公共资源的&#xff0c;如CSS、JS、图片等。你可以通过多种方式获取public目录的路径。 方法一&#xff1a;使用helper函数public_path() $path public_path(); 方法二&#xff1a;使用Request类 $path Request::root().…

centos远程桌面连接windows

CentOS是一款广泛使用的Linux发行版&#xff0c;特别是在服务器领域。很多企业和个人用户会选择远程连接到CentOS进行操作和维护。虽然CentOS自带了一些远程桌面解决方案&#xff0c;但它们在使用上存在一些局限性。接下来&#xff0c;我将介绍如何实现CentOS的远程桌面连接&am…

【C++ Primer Plus习题】16.8

大家好,这里是国中之林! ❥前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看← 问题: 解答: main.cpp #include <iostream> #include <set> #includ…

华为HarmonyOS地图服务 1 -- 如何实现地图呈现?

如何使用地图组件MapComponent和MapComponentController呈现地图&#xff0c;效果如下图所示。 MapComponent是地图组件&#xff0c;用于在您的页面中放置地图。MapComponentController是地图组件的主要功能入口类&#xff0c;用来操作地图&#xff0c;与地图有关的所有方法从此…

Python练习宝典:Day 1 - 选择题 - 基础知识

目录 一、踏上Python之旅二、Python语言基础三、流程控制语句四、序列的应用 一、踏上Python之旅 1.想要输出 I Love Python,应该使用()函数。 A.printf() B.print() C.println() D.Print()2.Python安装成功的标志是在控制台(终端)输入python/python3后,命令提示符变为: A.&…