J
Jorgen Grahn
Well I wouldn't. I hardly ever store Ts in a vector. More often than
not I use a container of shared_ptr<T>. Adding a copy constructor when
your class design doesn't warrant one is poor form.
I see now that the rest of the posters in the thread focused on the
copy constructor. I got stuck on the default constructor, because
that's where I have problems.
Most of my classes are small, concrete classes and are naturally
copyable. Default copying is enough -- and come to think of it, if the
default wasn't enough, then it would probably not be one of those
classes I'd consider placing in a container!
Consider what will
happen if you later need to add a reference member or a managed resource
to your class.
I consciously try /not/ to think of such things. A future drastic
redesign will simply have to have repercussions. Hopefully it never
has to happen -- the copyability was part of the larger design, not
accidental.
Using shared_ptr<T> removes most of those issues.
I think that last issue is enough for me.
std::Vector<HomeForSale> homes = lots_of_homes();
assert(!homes.empty());
const HomeForSale& home = homes[0];
assert(home.valid());
is not much worse compared to
std::Vector<shared_ptr<HomeForSale> > homes = lots_of_homes();
assert(!homes.empty());
shared_ptr<HomeForSale> home = homes[0];
assert(home.get());
In both cases I cannot statically know that I can pull a valid home
from the non-empty vector.
/Jorgen