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++


- C++ 协变模板 2021-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 近似搜索的工作原理 2021-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 静态初始化顺序失败 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 从python回调到c++的选项 2022-11-16
- Stroustrup 的 Simple_window.h 2022-01-01