如何测试引用是否为 NULL?
我想检查某些内容是否为空,例如:
string xxx(const NotMyClass& obj) {
if (obj == NULL) {
//...
}
}
但编译器对此抱怨: ==
有 5 种可能的重载。
所以我尝试了这个:
if (obj == static_cast<NotMyClass>(NULL)) {
这会崩溃,因为 NotMyClass
的 ==
重载不处理空值。
编辑:每个人都告诉我它不能为空,我肯定会在调试器中得到一些空值:
>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在格式良好的 C++ 程序中,引用永远不会是 NULL(更准确地说,您所引用的对象的地址可能永远不会是 NULL)。
因此,答案不仅是“不,没有办法”,而且推论是“这没有意义”。
您关于 C 的陈述也没有任何意义,因为 C 没有引用。
至于Java,它的“引用”在很多方面都更像C++指针,包括这一点。
比较不同语言之间的此类特定行为是一件愚蠢的事情。
如果您需要这种“可选对象”行为,那么您正在寻找指针:
但请考虑您是否真的需要这个;如果你确实这样做的话,一个不错的替代方案可能是
boost::optional
。In a well-formed C++ program, references are never
NULL
(more accurately, the address of an object to which you have a reference may never beNULL
).So not only is the answer "no, there's no way", a corollary is "this makes no sense".
Your statement regarding C makes no sense either, since C does not have references.
And as for Java, its "references" are more like C++ pointers in many ways, including this one.
Comparing such specific behaviours between different languages is something of a fool's errand.
If you need this "optional object" behaviour, then you're looking for pointers:
But consider whether you really need this; a decent alternative might be
boost::optional
if you really do.你问的没有意义。 C++ 中的引用永远不能为“null”,因为它们只能通过为现有对象添加别名来创建,并且不能重新绑定。一旦引用
x
,就始终引用x
。(如果原始对象的生命周期早于引用的生命周期结束,则引用可能会变成“悬空”,但这是一个编程错误,而不是可检查的运行时条件。)
What you're asking makes no sense. References in C++ can never be "null", since they can only ever be created by aliasing an existing object, and they cannot be rebound. Once a reference to
x
, always a reference tox
.(A reference may become "dangling" if the original object's lifetime ends before that of the reference, but that's a programming error and not a checkable runtime condition.)
正如人们所说,C++ 中的引用永远不应该为 null(NULL 或 nullptr),但是仍然有可能获得 null 引用,特别是如果您进行了一些邪恶的转换。 (很久以前,当我不知道更好的时候,我做了这样的事情。)
要测试引用是否为 null(NULL 或 nullptr),请将其转换为指针,然后进行测试。所以:
这就是您正在有效寻找的东西。
但既然您知道如何做,就不要。假设引用永远不会为空,如果是的话,让应用程序崩溃,因为到那时一定有其他事情发生了严重错误,程序应该被终止。
As people have said references in C++ should never be null (NULL or nullptr), however it is still possible to get null references, especially if you do some evil casting. (A long time ago I did such a thing when I didn't know any better.)
To test if a reference is null (NULL or nullptr) convert it to a pointer and then test. So:
is what you are effectively looking for.
But now since you know how to do it, don't. Just assume that references can never be null and let the application crash if they are, because by then something else must have gone horribly wrong and the program should be terminated.
您不需要对此进行测试,因为 C++ 中的引用不能为 NULL。 指针可以为NULL,但您在这里没有使用它们。
You don't need to test this, as references in C++ can't be NULL. Pointers can be NULL, but you're not using them here.
正如其他人所说,定义良好的代码永远不会有 NULL 引用,因此您没有责任测试它们。
严格来说,这并不意味着它们在实践中从未被创建过(但希望是在中间代码中,而不是在生产代码中)。在某些编译器中,虽然绝对不是标准 C++,但有可能获得地址为 NULL 的引用:
通常不会崩溃(目前),尽管按照 C++ 标准,第二行之后的行为是不确定的。这是通常使用“幕后”指针实现的引用的副作用。当有人使用 x 时,它可能会稍后崩溃。
如果您尝试调试这种情况,可以测试 x 的地址是否不为 NULL:
As others said, well-defined code never has NULL references, so it's not your responsibility to test for them.
That doesn't strictly mean they aren't ever created in practice though (but hopefully in intermediate, rather than production code). It's possible in some compilers, though definitely not standard C++, to get a reference whose address is NULL:
Often won't crash (yet), although by the C++ standard, it's nondeterministic behavior after the second line. This is a side-effect of references typically being implemented with pointers "behind the scenes." It will probably crash later down the line when someone uses x.
If you're trying to debug such a situation, you can test if the address of x is not NULL: