I am trying to represent an abstract syntax tree in C++. I have multiple classes which inherit base class 'TreeNode'. Some of them contain pointers to other inherited classes of TreeNode within them. With the method i am trying to use to get it to work, I can't seem to access the members within the child TreeNodes. Am i doing something wrong? Is there a better way to achieve this?
class TreeNode {};
class UnaryOperation : virtual public TreeNode {
public:
TreeNodeType Type;
UnaryOperationType Operation;
TreeNode* Operand;
UnaryOperation(UnaryOperationType operation, TreeNode* operand);
};
class BinaryOperation : virtual public TreeNode {
public:
TreeNodeType Type;
TreeNode* Left;
BinaryOperationType Operation;
TreeNode* Right;
BinaryOperation(TreeNode* left, BinaryOperationType operation, TreeNode* right);
};
. . .
TreeNode* unaryop_node = new UnaryOperation(op_type, operand_node);
return unaryop_node;
...
std::cout << (int)*unaryop->Type << std::endl;
class TreeNode has no member Type