On Nov 16, 10:44 am, Equus <chrism0...@gmail.comwrote:
Hi all,
I have a question concerning an index that I'm creating on a vector of
objects such that I can refer to the objects by integer. The index
will change, the vector *will not* (obviously, otherwise the pointers
would screw up). I have:
vector<MyObjectmyVector;
map<size_t, MyObject&myIndex;
Now, the compiler chucks at the declaration of myIndex giving "error:
reference to reference is not allowed". This is presumably because of
the reference to type MyObject in the map declaration. So, my
question is: is this a valid use of references in C++? Should I just
be using a plain old pointer to point to the various MyObjects in
myVector?
Thanks,
Chris
Just to be sure, you know that you can't update the vector beyond it's
reserved size after you've indexed it right, otherwise it'll
reallocate and your index will become invalid.
I guess the compiler is complaining that the map implementation is
using a T& for it's template parameter. In this case you're sticking
an object& into a reference parameter and, no, it's not valid. I'm not
sure if that implementation is required or if you're just unfortunate
with your stl implementation.
Given that the compiler won't let you use references, whether it's
valid within the standard or not, I'd say you need to change tactics.
Switch to a bare pointer or a smart pointer (maybe a reference counted
pointer in both the vector and the map?).