c++ - 引用返回和右值

标签 c++ c++11

我不明白书中的这段代码:

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 返回的指针。 -> 运算符的优先级高于 *(完整列表请参见 operator precedence),因此该语句实际上与 *(found-> second),而不是像您想象的那样 (*found)->second

关于c++ - 引用返回和右值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20364024/

相关文章:

c++ - namespace 的可见性问题

c++ - 与抽象函数的一致性

c++ - 错误 C2535 : member function already defined or declared

c++ - 在编译时检查字符的唯一性

C++:我可以将子对象放入父对象数组中吗?

c++ - 使用 C++ 中的 try 和 catch 方法创建一个函数以获取用户名

c++ - Boost::multi_array——引用太慢

c++ - std::is_pod 与子类化

c++ - 为什么要在方法的定义中使用 throw?

c++11 lambda 表达式问题(vs2012 update 1)