如何测试引用是否为 NULL?

发布于 2024-12-13 18:26:47 字数 455 浏览 0 评论 0 原文

我想检查某些内容是否为空,例如:

string xxx(const NotMyClass& obj) {
  if (obj == NULL) {
     //...
  }
}

但编译器对此抱怨: == 有 5 种可能的重载。

所以我尝试了这个:

  if (obj == static_cast<NotMyClass>(NULL)) {

这会崩溃,因为 NotMyClass== 重载不处理空值。

编辑:每个人都告诉我它不能为空,我肯定会在调试器中得到一些空值:

>

I want to check to see whether something is null, e.g.:

string xxx(const NotMyClass& obj) {
  if (obj == NULL) {
     //...
  }
}

But the compiler complains about this: there are 5 possible overloads of ==.

So I tried this:

  if (obj == static_cast<NotMyClass>(NULL)) {

This crashes because NotMyClass's == overload doesn't handle nulls.

edit: for everyone tell me it can't be NULL, I'm certainly getting something NULL like in my debugger:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

忘东忘西忘不掉你 2024-12-20 18:26:47

在格式良好的 C++ 程序中,引用永远不会是 NULL(更准确地说,您所引用的对象的地址可能永远不会是 NULL)。

因此,答案不仅是“不,没有办法”,而且推论是“这没有意义”。


您关于 C 的陈述也没有任何意义,因为 C 没有引用。

至于Java,它的“引用”在很多方面都更像C++指针,包括这一点。

比较不同语言之间的此类特定行为是一件愚蠢的事情。


如果您需要这种“可选对象”行为,那么您正在寻找指针:

std::string xxx(const NotMyClass* ptr) {
  if (ptr == NULL)
     throw SomeException();

  const NotMyClass& ref = *ptr;
  /* ... */
}

但请考虑您是否真的需要这个;如果你确实这样做的话,一个不错的替代方案可能是 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 be NULL).

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:

std::string xxx(const NotMyClass* ptr) {
  if (ptr == NULL)
     throw SomeException();

  const NotMyClass& ref = *ptr;
  /* ... */
}

But consider whether you really need this; a decent alternative might be boost::optional if you really do.

吃→可爱长大的 2024-12-20 18:26:47

你问的没有意义。 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 to x.

(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.)

随遇而安 2024-12-20 18:26:47

正如人们所说,C++ 中的引用永远不应该为 null(NULL 或 nullptr),但是仍然有可能获得 null 引用,特别是如果您进行了一些邪恶的转换。 (很久以前,当我不知道更好的时候,我做了这样的事情。)

要测试引用是否为 null(NULL 或 nullptr),请将其转换为指针,然后进行测试。所以:

if (&obj == 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:

if (&obj == nullptr)

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.

秋千易 2024-12-20 18:26:47

您不需要对此进行测试,因为 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.

丑丑阿 2024-12-20 18:26:47

正如其他人所说,定义良好的代码永远不会有 NULL 引用,因此您没有责任测试它们。

严格来说,这并不意味着它们在实践中从未被创建过(但希望是在中间代码中,而不是在生产代码中)。在某些编译器中,虽然绝对不是标准 C++,但有可能获得地址为 NULL 的引用:

int * p = NULL;
int & x = *p;

通常不会崩溃(目前),尽管按照 C++ 标准,第二行之后的行为是不确定的。这是通常使用“幕后”指针实现的引用的副作用。当有人使用 x 时,它可能会稍后崩溃。

如果您尝试调试这种情况,可以测试 x 的地址是否不为 NULL:

#include <cassert>
// ...
assert(&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:

int * p = NULL;
int & x = *p;

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:

#include <cassert>
// ...
assert(&x != NULL);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文