Boost logo

Boost Users :

From: George A. Heintzelman (georgeh_at_[hidden])
Date: 2002-01-17 12:12:05


> > 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.

Harder option: Write a new container class which doesn't supply dynamic
allocation, and thus changing capacity (templatize it on the maximum
capacity, and you can store the data on the stack) but does supply
resizing up to that maximum capacity, keeping unused elements
unintialized, calling the destructors appropriately, initializing new
elements with the copy constructor. Be aware that you won't be able to
sort the container, since the elements lack operator=, or use insert or
erase (pop_back and push_back only will work). You should supply this
insert and erase functionality anyway, in case someone uses your
container with a type supplying operator=.

Submit your new class to boost for others to use.

George Heintzelman
georgeh_at_[hidden]


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