Boost logo

Boost Users :

From: Tom Matelich (tmatelich_at_[hidden])
Date: 2002-01-17 17:36:11


>
> > > Also, I would recommend that you either reconsider
> > > having the constant member variable, or use a
> > > vector<>.
> >
> > Using a vector<T> won't work in this case, because the STL uses
> > T::operator = in order to copy vector contents into new
> locations during
> > memory reallocation. And there's no logical way to
> implement operator =
> > when a const data member is present, which is the cause of this
> > conversation.
> > I could use std::list, since it doesn't do reallocation, but clients
> > using this class in containers will most likely need random access
> > iterators.
> >
> > Any other ideas?
>
> Easy option: Use a vector<shared_ptr<T> >. This will give you
> basically
> the behavior you want, with the ability to initialize the elements
> directly. The drawback is that the data will be dynamically allocated.

Another drawback is the interface is now pointers, not references. If you
don't mind the dynamic allocation, the vector<shared_ptr<T> > can be
contained by another class, overloading operator[], etc. The Boost Iterator
Adapter library works very nicely for providing vector<T>::iterator behavior
from a vector<shared_ptr<T> >::iterator. I can send you our vector-like
class which uses adapted iterators if you would like.

Re: the moderation. Yes, this is not a boost question, but it is a question
boost can provide good solutions for, so where do you draw the line?


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net