Quote:Original post by JYQuote:Original post by snk_kid
If you don't declare your variables as of reference type then a copy will be made...
But this isn't true for vectors?
[rolleyes] what exactly do you mean? as i said before both of vector/map's overloaded subscript operators return constant and non constant reference to there elements, this is exactly the same semantics as C-style arrays in C++. Do you understand what l-values & r-values are?
When you access an element of an array of T in the context of an r-value the type is const T&.
When you access an element of an array of T in the context of an l-value the type is T& or const T& if the array has been declared constant.
If that still doesn't make sense take this example:
foo bar[N]; // a C-style array
foo foo_val = bar[0]; // a copy is made of the first element
const foo& const_foo_ref = bar[0]; // refers to the first element, no copy its the original.
foo& foo_ref = bar[0]; // refers to the first element, no copy its the original.
And the same holds for vector/map.