引用返回值和右值

Reference return and rvalue

本文关键字:返回值 引用      更新时间:2023-10-16

我在书中没有这段代码:

template <typename Resource, typename Identifier>
Resource& ResourceHolder<Resource, Identifier>::get(Identifier id)
{
    auto found = mResourceMap.find(id);
    assert(found != mResourceMap.end());
    return *found->second;
}

当变量是一个普通迭代器而不是指针时,为什么我们要取消对它的引用?然后访问它,就好像我们有int obj=new obj()这样的东西&obj->someVar;

此页上的cpp引用http://www.cplusplus.com/reference/iterator/表示可以将迭代器取消引用为右值。

我开始阅读这一页http://thbecker.net/articles/rvalue_references/section_01.html

这是一篇很好的文章,但有点密集,有人可以在我提供的示例代码的上下文中澄清这一点吗?

*found->second正在取消引用found->second返回的指针。->运算符的优先级高于*(有关完整列表,请参阅运算符优先级),因此该语句实际上与*(found->second)相同,而不是像您所想的那样与(*found)->second相同。