What is a good way to impose circular inheritance here?
class Node {
Error (gcc 4.7.0) because TrafficLight
is an incomplete type when HasImportance
tries to inherit it.
The real problem is that HasImportance needs to know the type returned by neighbors()
. If HasImportance
inherits from Node
, then it thinks that neighbors()
returns a list of Node*
, not TrafficLight*
, and therefore do not know what it can call receive_importance()
for elements. Similar if HasImportance
does not inherit at all.
By the way, I am trying to make several mixes to help identify many different types of graphs to easily and individually test each mixture separately. For example, I would have to define a node class for a traffic light graph by simply writing something like class TrafficLight : public HasImportance, HasState<3>, virtual Node { }
.
I have three ways to solve this, but everyone seems ugly. (1) static_cast<NodeType*>
. (2) TrafficLight
passes this
to HasImportance
in its constructor. Here, HasImportance
need not be inherited at all; it just stores the pointer to (s) itself, and the template parameter provides a pointer type. (3) Create a Node
template template, for example:
template<class NodeType> class Node { public: list<NodeType*> neighbors() { } } class TrafficLight : public HasImportance<Node<TrafficLight>> { }
It compiles and doesn't introduce a free copy of this
, but it seems ... a little too curious.
Is there a smell of code here? Should I approach these charts in a completely different way?
source share