Classes with reference members in a vector.

?

=?ISO-8859-15?Q?Juli=E1n?= Albo

I don't think that what I want to do is particularly exotic, to be
honest. C++ has references which is nice. It has the STL, which is
nice. It's just a pity to conclude that they don't get along too well.

When you need something similar to a reference but want to be able to make
it refers to some other thing, you can use a pointer. If you want to store
objects in a standard container, the objects must follow the requisites. If
you don't want to meet the requisites or don't want to use pointers you can
use another type or container. Nothing exotic, just using the more
convenient tool for the job.
 
W

werasm

werasm said:
would be required - what if inplace construction through exception?

line above should read:
would be required - what if inplace construction throws an exception?


Regards,

Werner
 
J

Jerry Coffin

[ ... ]
I would have expected that a copy constructor call would be executed in
the offended line instead of an assignment.

The standard containers require that any object stored in them be
copyable and assignable. Regardless of the why it's that way, if your
class isn't copyable and assignable, then you can't store it in a
standard container. I suppose it might work sometimes with some
implementations under some circumstances, but if so, it's purely
accidental.
 
H

Howard

A swap would be much preferable, you are right. Which makes another
interesting case - it will be impossible to swap two instances of
classes that have reference-type members.

That was my whole point.
I have a hard time thinking of scenarios where the combined default
constructor and operator= would outperform a well-written copy
constructor?

Who said anything about default construction? I was comparing
construction+destruction against assignment. Why would you have to create a
new object in order to call operator=()?
Still, a vector of /references/ would often come in handy I suppose.
And, in my particular app I'm trying to store pairs of references -
which would have been quite cheap. It's now just a vector of pointer
pairs.

I'm not sure how references are any "cheaper" than pointers.

But if you really want your objects to hold references, I'm still unsure why
you aren't storing pointers to those objects in the vector. Then all you
have to do is traverse the vector, using the pointers to access the objects
which hold the references. It's mostly just a difference between using "."
and using "->" to access the obects' members.

(Naturally, you have to manage the creation/destruction of the objects
appropriately when using pointers, but a good smart pointer such as Boost
provides will help there, too.)

-Howard
 

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,904
Messages
2,570,003
Members
46,359
Latest member
thejackson123

Latest Threads

Top