Question

I was wondering how I'd represent a template class inheriting from another template class in UML?

Something like this:

class non_templated {
   ...
};

template<typename x>
class templated_a {
    ...
};

template<typename y>
class templated_b
  : public templated_a<y>,
    public non_templated {
    ...
};

Thanks! :)

Was it helpful?

Solution

Yo have class templated_b that inherits from non_templated and from templated_a. The template parameter of templated_a is the same as the one of templated_b. In this case I think that it is sufficient to draw both classes as having a parameter with the same name:

Class diagram 1

In my opinion it is obvious that Y is the same parameter, so I prefer to keep the diagram as clean as possible and not add additional notations.

But there are also different opinions. This question is from someone who is afraid that people might not find that obvious: "how to know Y of templated_b is the same type than Y of templated_a?"

If you have the same concerns, than you can use the UML template binding notation.

"Template binding is shown as a dashed arrow decorated with the keyword «bind» and binding information, with the hollow triangle arrowhead, and directed from the bound element at the tail to the template."

Here you can read more about it.

Using this notation your diagram would look like this:

Class diagram with template binding

I don't like that, because it uses two different types of lines for inheritance and I think that most people wouldn't understand the template binding notation.

An alternative would be to add a <<bind>> stereotype and to the generalization line and name it as <Y->X>. This makes the generalization line resemble to the template binding notation. Something like:

enter image description here

Even though this is not formal notation for template binding I prefer it to the second diagram. And I prefer even better the first. I think it is the diagram most people would understand better. You could even include the binding parameters information in the stereotype: <Y>>`. It's up to you to select what is better understood by the people for which the diagram is drawn.

Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top