简体   繁体   English

从函数返回对指针的引用与返回指针相同?

[英]Returning a reference to a pointer from a function same as returning a pointer?

I have a feeling that the two examples in the following are equivalent, that is, returning a reference to a pointer and returning a pointer are the same thing. 我感觉下面的两个示例是等效的,也就是说,返回对指针的引用和返回指针是同一回事。 It sounds strange to say this, that a reference to pointer and pointer are the same thing, but I think it is the case in this example: 这么说听起来很奇怪,对指针和指针的引用是同一回事,但我认为在此示例中就是这种情况:

#include <unordered_map>

struct Animal{};

std::unordered_map<std::string, Animal*> map;

Animal* createNewMapEntry()
{
    return map["new entry"] = new Animal; // map subscript operator will return a reference to the mapped type
                                        // in this case the mapped type is a pointer to Animal
    // Is this the equivalent of doing:
    auto p = new Animal;
    map["new entry"] = p;
    return p;
    // In this case I am returning a pointer to Animal, not a reference to the pointer to animal.
    // That's why I was afraid that in the first example, which returns a "reference" to the Animal pointer
    // that it was returning the "address" of the pointer instead of the pointer, equivalent to returning 
    // a pointer to pointer, which is just like the address to a pointer.
}

If this is true, I think this is a really confusing part of the language, at least for me. 如果这是真的,我认为这是语言的一个真正令人困惑的部分,至少对我而言。

I have a feeling that the two examples in the following are equivalent. 我觉得以下两个示例是等效的。

No , they are not. ,不是。

The first inserts to the map, with key "new entry" and value an Animal object. 第一个插入到地图中,键为“新条目”,并为Animal对象赋值。

The second one doesn't do this, since it just returns the pointer. 第二个不执行此操作,因为它仅返回指针。


As MM said: "If a function returns by value and you return an lvalue, the lvalue is converted to prvalue , so the return statement just returns the pointer". 就像MM所说的:“如果一个函数按值返回并且您返回一个左值,则左值将转换为prvalue,因此return语句仅返回指针”。

The contents of the compound statement that is the function body have no effect on the return type of the function (unless the return type is auto ). 作为函数体的复合语句的内容对函数的返回类型没有影响(除非返回类型为auto )。

The return type is Animal* , which is not a reference type. 返回类型是Animal* ,这不是引用类型。 The examples are semantically equivalent. 这些示例在语义上是等效的。

Conversion of T& to T in expressions is a fundamental aspect of C++. 的转换T&T在表达式是C ++的一个基本方面。 It's one of the first things that happens during expression evaluation. 这是在表达式求值过程中发生的第一件事。 See [expr]/5 : 参见[expr] / 5

If an expression initially has the type “reference to T ” ([dcl.ref], [dcl.init.ref]), the type is adjusted to T prior to any further analysis. 如果表达式最初的类型为“对T引用”([dcl.ref],[dcl.init.ref]),则在进行任何进一步分析之前,将类型调整为T The expression designates the object or function denoted by the reference, and the expression is an lvalue or an xvalue, depending on the expression. 表达式指定由引用表示的对象或功能,并且表达式取决于表达式是左值还是x值。

Since the result of map["new entry"] is Animal*& (and the assignment expression returns an lvalue referring to the left operand ), the & in the return expression disappears, leaving an lvalue Animal* . 由于map["new entry"]Animal*& (并且赋值表达式返回一个引用左操作数左值 ),因此return表达式中的&消失,剩下一个左值 Animal* After that, since the expected return type of createNewMapEntry() is Animal* , an lvalue-to-rvalue conversion takes place and a copy of the pointer is returned. 之后,由于createNewMapEntry()的预期返回类型为Animal* ,因此将进行左值到右值转换,并返回指针的副本

Note - the return type is always Animal* as declared, you never " return a reference " in this example. 注意-返回类型始终是声明的Animal* ,在此示例中,您永远不要“ 返回引用 ”。 In both cases a pointer is returned. 在两种情况下都返回一个指针

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM