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. 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@aya.yale.edu