Recently, I had to implement an abstract query tree for a research project. The main object is ASTNode, but the actual tree nodes were derived classes: expression nodes, comparison nodes, operand nodes, etc. Each node type has its own members, such as the node (s) operand, argument lists, etc.
The parsing functions that recursively generate this tree should return types ASTNode, since they simply return a pointer no matter what type of node is in the root of the generated tree.
This means that the functions that analyze the tree must ask for each node in the tree what type it is before working with it. This can be done using dynamic throws or typeid, but SO and Google style rules will be angry at my violation of the Liskov signature principle and all type type checks in general.
I cannot put functions that work with nodes in the subclasses of node itself, as trees can be used in several different ways in different parts of the application. Is there a better way to structure my trees?
source
share