Is there a C++ equivalent of a NullPointerException(是否有一个相当于 NullPointerException 的 C++)
问题描述
Java 和 C#,可能还有许多其他语言,都有一个预定义的异常类,当在不应该使用的地方使用空参数时会抛出该异常类.C ++中有类似的东西吗?如果没有,我可以使用其他预定义的异常还是应该定义自己的异常?
取消引用 NULL 指针是 C++ 中的未定义行为 - 这意味着代码看起来可以工作.不保证会抛出异常.您可以使用
std::invalid_argument
>
异常(为其提供一个有意义的值 - p 为 NULL"
),但您必须自己进行检查.
Both Java and C#, and probably many other languages too, have a predefined exception class that is thrown when a null parameter is used where it should not. Is there anything similar in C++? If not, is there another predefined exception I can use or should I define my own one?
Dereferencing a NULL pointer is undefined behaviour in C++ - which means the code can appear to work. An exception isn't guaranteed to be thrown. You can use the
std::invalid_argument
exception (provide a meaningful value to it - "p is NULL"
), but you'll have to do the check yourself.
这篇关于是否有一个相当于 NullPointerException 的 C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:是否有一个相当于 NullPointerException 的 C++


基础教程推荐
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01