mardi 28 juin 2016

If x is a reference to unique_ptr, then *x is "reference to element pointed to"?

refPtrInt is a reference to pointer:

int *& refPtrInt;

Is *refPtrInt an int or a reference to an int?

EDIT: Thank you for yours answers about refPtrInt. Actually, my original question is about this snippet from Stroustrup (A Tour of C++, section 5.5):

template<typename C, typename 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
}
vector<unique_ptr<Shape>> v;
// populate v with some kinds of Shape
for_all(v, [](Shape& s){ s.draw(); });

The comment "pass op() a reference to each element pointed to" is from Bjarn Stroustrup too. So, *x is apparently a reference to a Shape, when x is a reference to a unique_ptr<Shape>... Is that correct?

Aucun commentaire:

Enregistrer un commentaire