我不明白书中的这段代码:
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/