I second Georges recommendation about using shared pointers. I would say that the problem you described falls into the category of how to use STL for classes which are not STL compatable because of the way they copy or construct, etc. Really it is confusing for people starting up with STL, and it is never covered very well in documentation, i.e., clear warning about what you shouldn't put in a vector. vector<shared_ptr<T> > takes care of all of these cases in a single swoop, relieving the need for deeper anaysis and saving lots of time. Craig Hicks KGK Tokyo George A. Heintzelman wrote:
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.
[Non-text portions of this message have been removed]