References stored in maps

E

Equus

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<MyObject> myVector;
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
 
J

Jonathan Lane

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<MyObject> myVector;
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?).
 
G

Guest

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<MyObject> myVector;
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?

You can not store references in a container so in situations like these
you have to either use some kind of pointer or rethink your design.
 
H

Heck

Equus!
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<MyObject> myVector;
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

I've successfully used maps with int, pointer pairs, e.g., <int, XX *>
Why not just use a pointer to your MyObject rather than a reference?
 
E

Equus

I've successfully used maps with int said:
Why not just use a pointer to your MyObject rather than a reference?

Yup, cheers all. I've defaulted to using pointers. Works fine :)

Chris
 
R

Ron Natalie

Equus said:
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?

The type passed to a container must be copy constructible and
assignable. References do NOT meet these requirements.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top