vendredi 2 août 2019

range based for loop with an auto reference to a pointer

With regard to the following code I would like to have some clarification. We have an array of pointers to a class. Next we loop over the array using a range based loop. For this range based loop auto& is used. But next when we use the element a we can use the arrow operator to call a function.

This code is compiled using C++ 11.

// Definition of an array of pointers to some class.
some_class* array[10];

// The array of pointers is set.

// Loop over the array.
for(auto& a : array) 
{
  // Call some function using the arrow operator.
  a->some_func();
}


Is my understanding correct that auto& a is a reference to a pointer? Is this not a bit over kill. Would using auto a not create a copy of the pointer and take up the same amount of memory?

Aucun commentaire:

Enregistrer un commentaire