Using reference in C ++ 11 for

I admit that my C ++ skills are a bit rusty and I dive right into C ++ 11 for the new project I'm about to start. I just found this confusing behavior if I write

void MyClass::update() { for(SomeClass &i : _list) { i.doStuff(); } } 

or

 void MyClass::update() { for(SomeClass i : _list) { i.doStuff(); } } 

seems to work the exact same way, <<22> is std::list<SomeClass> . Therefore, I was interested in what is used here, since I obviously missed something. I must mention that I am using Apple LLVM 4.0, which comes with Xcode 4.4.1 (4F1003).

Please feel free to taunt and make fun of me if this is a rudely stupid question, I really could use a good patting head :)

+4
source share
1 answer

The link has nothing to do with the container, but with the element that you extract from it. In the first case, you get access to the element that is in the list and change it, in the second case you make a copy of the element, and then change the copy, which in any case is beyond the scope.

+4
source

Source: https://habr.com/ru/post/1434443/


All Articles