Glen Able said:
Thanks to all the respondents for some interesting perspectives!
I've concluded that I'll just do a typedef in most cases, simply because I
find it easier to mentally parse 'StuffVector' rather than
'std::vector<Stuff>'.
I feel similarly to you here, Stephen:
But I'm wondering why you say this:
In the case where I want to extend the functionality e.g. add some method
like StuffVector::FindTheBestStuff(), I'm still not keen on creating a
simple wrapper class, because it's such a pain to then have to wrap all the
vector::begin() type stuff where that's required. Isn't it natural here to
derive StuffVector from std::vector<Stuff> and add whatever else I need?
First of all, hopefully you are aware that std::vector does not have a
virtual destructor, so you would have to make sure you never deleted a
StuffVector polymorphically. ie
std::vector<Stuff>* p = new StuffVector;
delete p; // undefined behaviour, never do this !!
This is not a reason to never derive from std containers, but it's
something you need to know.
To answer your question, presumably your implementation of
StuffVector::FindTheBestStuff() would only use the public interface of
the std::vector<Stuff> base class (there is no protected interface and
you can't get at the private bits). In which case I think the natural
solution is to implement FindTheBestStuff as a free-standing function
that takes a std::vector<Stuff> (probably by const reference) plus any
other parameters it needs and returns whatever
StuffVector::FindTheBestStuff() would have returned.
You might be interested to read this article by Scott Meyers about
free standing functions and encapsulation. I came across it after
finishing a project where I had decided go down the route equivalent
to having a StuffVector class with a std::vector<Stuff> member. During
the course of the project my design had crumbled around me somewhat,
and the code had got hideously monolithic (and I don't think the
inheritance solution would have helped). I seriously wished I had read
the article before I started.
http://www.cuj.com/documents/s=8042/cuj0002meyers/