lundi 12 août 2019

why use references for mutable variables in c++

I'm new to c++. I came across some code and got confused

vector<int> vec{3,1,4,1,5};
vector<int> &vecRef = vec;
auto vecCopy = vecRef; // makes copy of vec
auto &vecRef2 = vecRef; // reference

I read about the usage of reference types in c++ and I understand why it's useful for immutable types. But for mutable types like vectors, what's the difference between vector vecCopy = vec and vector& vecRef = rec? Aren't they both alias to vec?

Aucun commentaire:

Enregistrer un commentaire