Restrict template to class type

A

Arne Claus

I've got a tricky problem here.
I try to create an object-hierarchie, based on a template base class like

template <class T> class Node {
addChild(Node<T> *) =0;
// the Node-Container wil be defined in the base classes
};

The problem is, that the classes derived from Node represent different
forms of hierarchies, which all can use a different paramter T. This
paramter T however has to be a class dervied from a specific base class
(because I need specific functions in some member functions).
The Idea is, that hierarchies of a parameter T can only add other
hierarchies that use the same parameter T (to e.g. save conversion
operations).

template <class T> class HierarchieTypeA : public Node<T> {
};

template <class T> class HierarchieTypeB : public Node<T> {
};

HierarchieTypeA<a> obj1a; // where a is derived from "base"
HierarchieTypeB<a> obj2a;
HierarchieTypeA<b> obj1b; // where b is derived from "base"
HierarchieTypeB<b> obj2b;

obj1a.addChild(obj1a); // allowed
obj1a.addChild(obj2a); // allowed
obj1a.addChild(obj1b); // not allowed
obj1a.addChild(obj2b); // not allowed

The problem is, that I see no elegant way to prevent the template
paramter from being an int or float or whatever. I thought of
specialising the Node container like

vector<Node<base>*>

But that would involve a lot classname lookups and castings in some cases.
So if somebody knows a solution for this I would really be thankfull ^^

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top