Re: Template hierarchy question
On Mar 19, 2:31 pm, Inso Reiges <Insorei...@gmail.com> wrote:
I`m not a native english speaker, so please bear with me.
I`ve inherited a template library codebase. This library is
using a mixin approach and passes a concrete class type down
the inheritance tree in template parameter, as such:
template <class T> class Base {};
template <class T> class Der1 : public Base<T> {};
....
template <class T> class DerN : public DerN_minus_1<T> {};
The given hierarchy is simplified and uses multiple inheritance in
real code.
I have to make concrete (non-templated) classes on top of this
hierarchy:
class C1 : public DerN<C1> {};
class C2 : public DerN<C2> {};
...
Now, i have a collector class, that has to register all those C
classes somewhere inside it, like this:
class Collector { private: std::list<SomeType> mylist; };
But, as you know, i can not use templates for SomeType,
because compiler needs a concrete type. But i have to register
C classes in a single collection and C classes have to be on
top of the template inheritance hierarchy.
Could you please advise a solution?
You can't do it directly, since Base isn't a class. You have
two basic possible solutions:
-- use something like boost::any, or
-- create an artificial polymorphic base class, multiply
inherit from it in your concrete classes, and maintain a
list of pointers to it, e.g.:
class Listable
{
public:
virtual ~Listable() {}
} ;
class C1 : public DerN<C1>, public Listable {} ;
class C2 : public DerN<C2>, public Listable {} ;
// ...
std::list< Listable* > myList ;
This means, of course, that you'll have to handle lifetime
issues yourself, however.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34