是否有C ++等价的NullPointerException [英] Is there a C++ equivalent of a NullPointerException
问题描述
Java和C#,以及可能许多其他语言,都有一个预定义的异常类,当使用null参数时,抛出异常类。 C ++中有什么类似的东西吗?
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?
推荐答案
解除引用NULL指针是未定义的在C ++中的行为 - 这意味着代码可以看起来工作。不能保证抛出异常。您可以使用
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
异常(为它提供有意义的值 - p is NULL code>),但你必须自己做检查。
exception (provide a meaningful value to it - "p is NULL"
), but you'll have to do the check yourself.
这篇关于是否有C ++等价的NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!