TITLE: templates in an inheritance hierarchy (Source: comp.std.c++, 15 Mar 2000) ABBOTT: "Olinga K. Abbott" > Templates have been presenting me with a problem when used in combination > with inheritance. Specifically, I need to know how to inherit: 1) a > non-template class from a template class; ELT: Christopher Eltschka Well, there's no template class, but only a class template. That may sound like nit-picking, but the difference is important - you immediatly see that deriving a class from a template makes no sense: a class is always derived from a class. Now, a class template is a means for generating classes. Of course you can derive a class from another class which was generated through a template. The name of that class is given by the template name, followed by the template parameters. Since this is just a normal class, you can use it in any way as a normal class, including deriving from it, with exactly the same syntax. That is, if you have template class X { ... }; then you have a class template X, from which you can instantiate (= generate) classes X, X or even X< X >. To derive from such a class, you just use it like any other class: class Y: public X // Derived from class X { ... }; A very interesting special case is if you instantiate the template on the class you are currently defining: class Z: public X { ... }; ABBOTT: > 2) a template class from a > non-template class; ELT: Again, if you consider that it's a class template, the only reasonable interpretation is: "a template for a class which is derived from another class". Therefore you write: template // we want a template parametrized on a type // which we call T class A: // The classes shall be named A<...> public Y // and shall be derived from class Y { ... }; You also can derive from the template parameter: template class B: public T // The class shall be derived from the parameter // used to instantiate the template (of course, that // must be a class in this case) { ... }; ABBOTT: > and 3) inherit a template class from a template class. I > would like to see specific syntax, not just a general description. ELT: Not this is a template for a class which is derived from another class which was instantiated from another template. template class C: public X { ... }; Of course, the base class can (and most often will) depend on the template parameter given: template class D: public C { ... }; _______________________________________________ cpptips mailing list http://cpptips.hyperformix.com