1

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?

1
  • This is fine. NodeMap<node> is a perfectly normal class, just with a slightly odd spelling of its name.
    – BoBTFish
    Dec 5, 2012 at 12:12

3 Answers 3

4

You have the following possibilities as far at the code you've shown tells me what you are doing:

  1. Specialize Nodemap for T=node, to include the additional methods you want to have. For convenience you can then typedef Nodemap<node> to Matching. However this might add some code duplication of the Methods you have in the nonspecialized template.
  2. Derive from Nodemap<node> as you suggested. This is a valid approach, if you design Nodemap in 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.
  3. If possible, provide the additional functionality as free functions, so there is no need to derive from or specialize 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 of NodeMaps and Matching, then derive the NodeMap template from the corresponding base class template (probably without adding much functionality) and derive Matching from NodeMapBase<node>. If you have a look at MSVC's standard library implementation you see that a lot there.

2
  • What would be the benefit of having a common base class NodeMapBase? Currently, I see a Matching as a NodeMap<node> with added functionality, so the benefit of introducing another base class is not clear to me.
    – clstaudt
    Dec 5, 2012 at 12:27
  • 1
    If you see no benefit, simply don't use it. Sometimes it's a viable option, e.g. if a Matching needs a base class constructor that a NodeMap does not provide - then you can provide that constructor in your base class. In such cases, the inheritance is a pure technical way to avoid code duplication, not to express a real "is-a" relation.
    – Arne Mertz
    Dec 5, 2012 at 12:39
2

Yes, it's commonly used.

class Matching : public NodeMap<node> {
    ...
};
2

This depends on how the interface of Matching should look like. If you want to provide exactly the same methods plus some convenience methods, you can inherit it

class Matching : public NodeMap<node> {...};

But if Matching is not a NodeMap<node>, i.e. provides a different interface, you should only use it as an aggregate

class Matching {
private:
    NodeMap<node> matchingNodes;
public:
    // public interface of Matching
    bool isProperMatching(Graph& G);
    bool match(node u, node v);
    ...
};

BTW, this advice is independant of NodeMap being a template or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.