0
我在读“C++编程语言(第4版)”,我就遇到了这个:解引用的参考
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
?
查看注释,它说“C”是指针的容器。显然'op()'不需要指针,它需要指针指向的值。所以你必须间接。 – Barmar