I have defined a class
template <class T> class NodeMap {
NodeMap(int n, T defaultEntry = NULL);
virtual ~NodeMap();
T& operator[](const node& u);
...
}
which maps an object of type node to an object of parameter type T.
Now I’d like to have a class Matching, which is essentially a NodeMap<node>. For convenience, I’d like to add methods like isProperMatching(Graph& G) and match(node u, node v). Can Matching inherit from NodeMap<node>? Is it possible (and if yes, is it a good idea) to extend a template class with a fixed template parameter?
You have the following possibilities as far at the code you’ve shown tells me what you are doing:
NodemapforT=node, to include the additional methods you want to have. For convenience you can then typedefNodemap<node>toMatching. However this might add some code duplication of the Methods you have in the nonspecialized template.Nodemap<node>as you suggested. This is a valid approach, if you designNodemapin a way that makes it a proper base class. The virtual destructor suggests you did that already, although it might not be necessary to make methods virtual at all.Nodemap<node>I would consider a mixed approach of the first two: Make a base class template (I’ll call it
NodeMapBase) that contains the common functionality ofNodeMaps andMatching, then derive theNodeMaptemplate from the corresponding base class template (probably without adding much functionality) and deriveMatchingfromNodeMapBase<node>. If you have a look at MSVC’s standard library implementation you see that a lot there.