Re: Proper use of templated containers as class members
ma740988 wrote:
On Dec 4, 5:15?am, Per <p...@isy.liu.se> wrote:
public:
//Constructors and stuff
const directory_t& directory() const {return directory_m;}
};
. Classes should of course hide their
implementation but it seams very stupid not to have read access
directly to a member. If I didn't have that I have to bloat the class
with wrapper functions for the members.
I often wonder about this myself. Frankly this is one case where I
cant see the value added.
It is somewhat analogous to using unnamed numerical constants (magic
numbers) vs. named numerical constants. So, as
struct X {
static unsigned long const number_of_cycles = 13;
};
is better than using 13 throughout the code, using X::directory_t instead of
std::map<std::string, int> is better for the same reasons. Think of using
std::map<std::string, int> as "magic typing".
Now, the documentation may even describe directory_t as implementation
defined and conforming to a certain interface of which map<string,int> is a
model. In that case, using map<string,int> as an implementation of
directory_t is not wrong; and client code that relies on this particular
implementation is broken (in the same way as vector<T>::iterator could be
implemented as T* and client code relying on this implementation is
broken). However, it is true that from a quality of implementation point of
view it would probably be better to do something like:
struct directory_t : private map<string,int> {
// some using ... to forward the parts of the interface
// required by directory_t
//
// some constructors
};
Best
Kai-Uwe Bux