取消引用引用
Dereferencing a reference
我正在阅读 "The C++ Programming Language (4th edition)" 并且我 运行 阅读了这篇文章:
template<class C, class Oper>
void for_all(C& c, Oper op) // assume that C is a container of pointers
{
for (auto& x : c)
op(*x); // pass op() a reference to each element pointed to
}
据我了解,我们正在迭代 c
并获取对 x
的引用,这是当前迭代。 x
然后传递给 op
的函数调用运算符,但它首先被取消引用?为什么 x
会被取消引用?
您在发布代码的评论中说:
// assume that C is a container of pointers
这意味着x
是对指针的引用。 *x
评估为指针指向的对象。
op
必须期望对象或对对象的引用,而不是指向对象的指针。
我正在阅读 "The C++ Programming Language (4th edition)" 并且我 运行 阅读了这篇文章:
template<class C, class Oper>
void for_all(C& c, Oper op) // assume that C is a container of pointers
{
for (auto& x : c)
op(*x); // pass op() a reference to each element pointed to
}
据我了解,我们正在迭代 c
并获取对 x
的引用,这是当前迭代。 x
然后传递给 op
的函数调用运算符,但它首先被取消引用?为什么 x
会被取消引用?
您在发布代码的评论中说:
// assume that C is a container of pointers
这意味着x
是对指针的引用。 *x
评估为指针指向的对象。
op
必须期望对象或对对象的引用,而不是指向对象的指针。