引用可以为空吗?

发布于 2024-08-19 07:06:47 字数 330 浏览 4 评论 0原文

我从维基百科上读到:

“引用不能为空,而指针可以;每个引用都引用某个对象,尽管它可能有效也可能无效。”

但是,我不相信这一点,因为以下代码编译时没有错误:

class person
{
    public:
    virtual void setage() = 0;
};

int main()
{
    person *object = nullptr;
    person &object1 = *object;
}

I have read from the Wikipedia that:

“References cannot be null, whereas pointers can; every reference refers to some object, although it may or may not be valid.”

However, I don’t believe this because of following code, which compiles with no error:

class person
{
    public:
    virtual void setage() = 0;
};

int main()
{
    person *object = nullptr;
    person &object1 = *object;
}

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

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

发布评论

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

评论(7

回忆那么伤 2024-08-26 07:06:47

在您的代码中:

person *object=NULL;
person &object1=*object;

您取消引用 NULL 指针,因此您会得到未定义的行为。为了回答你的问题,不存在 NULL 引用这样的东西。

为了解决问题的另一部分,仅仅因为程序可以编译,并不能保证它是正确的或它会工作。 C++ 编译器甚至不需要尝试诊断代码包含的错误类型。

In your code:

person *object=NULL;
person &object1=*object;

you dereference a NULL pointer, so you get undefined behaviour. And to answer your question, there is no such thing as a NULL reference.

And to address the other part of your question, just because a program compiles, there is no guarantee that it is correct or that it will work. C++ compilers are not required to even attempt to diagnose the kind of error your code contains.

薄暮涼年 2024-08-26 07:06:47

person &object1=*objectperson &object1=NULL 不同。也许编译器不够聪明,无法发现您正在取消引用空指针,但无论如何您都会收到运行时错误。所以它们仍然是真实的;)

Saying person &object1=*object is not the same thing as saying person &object1=NULL. Probably the compiler is just not smart enough to find out that you are dereferencing null pointer, but you'll get a runtime error anyway. So they are kind of true still ;)

那小子欠揍 2024-08-26 07:06:47

您可以有一个空引用,不知道为什么有人会说,否则,这是某些操作的令人讨厌的副作用。您只是无法直接创建一个。

You can have a null reference, not sure why anyone would say otherwise, it is a nasty side effect of some operations. You just can't create one directly.

夏末 2024-08-26 07:06:47

那会让你的程序崩溃。你尝试运行它吗?
执行 *object 将遵循空指针,因此实际上您的引用永远不会被分配。

that would crash your program. Did you try running it?
doing *object will deference a null pointer, so in fact your reference never gets assigned.

风和你 2024-08-26 07:06:47

好吧,您可以在 C++ 中做您想做的任何事情。另一个例子:

person &object1 = *( reinterpret_cast<person*>(0) );

除了您提到的情况之外,您在上述情况下还调用了未定义的行为!

Well, you can do whatever you want in C++. Another example:

person &object1 = *( reinterpret_cast<person*>(0) );

You are invoking an undefined behavior in the above case, beside the case you mentioned!

不再见 2024-08-26 07:06:47

clang 3.5 甚至对稍后可能对引用进行 NULL 检查发出警告:

/tmp/person.C:11:6: warning: reference cannot be bound to dereferenced null pointer in well-defined C++ code; pointer may be assumed to
      always convert to true [-Wundefined-bool-conversion]
if (&object1) {}
~~   ^~~~~~~
1 warning generated.

clang 3.5 even warns on a possible later NULL check of a reference:

/tmp/person.C:11:6: warning: reference cannot be bound to dereferenced null pointer in well-defined C++ code; pointer may be assumed to
      always convert to true [-Wundefined-bool-conversion]
if (&object1) {}
~~   ^~~~~~~
1 warning generated.
仅冇旳回忆 2024-08-26 07:06:47

gcc8 会给出警告:

警告:编译器可以假设“object1”的地址永远不会为 NULL [-Waddress]

一个小演示:

#include <iostream>

class person
{
    public:
        virtual void setage()=0;
};

int main()
{
    person *object=NULL;
    person &object1=*object;

    if (&object1 == NULL) {
        std::cout << "NULL object1" << std::endl;
    }

    if (!(&object1)) {
        std::cout << "NULL object1 " << std::endl;
    }
}

编译并运行输出:

g++ -std=c++2a -pthread -fgnu-tm -O2 -Wall -Wextra -学究式 -pthread
-迂腐错误 main.cpp -lm -latomic -lstdc++fs && ./a.out

main.cpp:在函数“int main()”中:

main.cpp:14:18:警告:编译器可以假设地址
'object1' 永远不会为 NULL [-Waddress]

 if (&object1 == NULL) {

              ^

main.cpp:18:19:警告:编译器可以假设地址
'object1' 永远不会为 NULL [-Waddress]

 if (!(&object1)) {

               ^

空对象1

空对象1

gcc8 will give a warning about it:

warning: the compiler can assume that the address of 'object1' will never be NULL [-Waddress]

A small demo:

#include <iostream>

class person
{
    public:
        virtual void setage()=0;
};

int main()
{
    person *object=NULL;
    person &object1=*object;

    if (&object1 == NULL) {
        std::cout << "NULL object1" << std::endl;
    }

    if (!(&object1)) {
        std::cout << "NULL object1 " << std::endl;
    }
}

Compile and running output:

g++ -std=c++2a -pthread -fgnu-tm -O2 -Wall -Wextra -pedantic -pthread
-pedantic-errors main.cpp -lm -latomic -lstdc++fs && ./a.out

main.cpp: In function 'int main()':

main.cpp:14:18: warning: the compiler can assume that the address of
'object1' will never be NULL [-Waddress]

 if (&object1 == NULL) {

              ^

main.cpp:18:19: warning: the compiler can assume that the address of
'object1' will never be NULL [-Waddress]

 if (!(&object1)) {

               ^

NULL object1

NULL object1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文