I am having a tree data structure that can have different types of nodes deriving from one TreeBaseNode class. I have made the tree nodes of type TreeBaseNode * which gets assigned to objects of different Special nodes at run time.
How can I access aNodeAttribute from the TreeBaseNode* pointer while traversing the tree on the tree is generated (if the node is of type SpecialNodeA which I can identify from NodeType)
class TreeBaseNode
{
public:
int NodeType;
TypeA commonNodeAttributeA,
TypeB commonNodeAttributeB;
};
class SpecialNodeA : public TreeBaseNode
{
private:
TypeD aNodeAttribute
public:
SpecialNodeA(int type)
{
NodeType = type;
}
//Methods
};
class SpecialNodeB : public TreeBaseNode
{
private:
TypeE bNodeAttribute;
public:
//Methods
};
EDIT: changed the types
NodeTypeI want to use the attributes to estimate something. It is done outside the class while traversing through the nodes. For instance if it is aSpecialNodeA, I would call a function withcommonNodeAttributeA,commonNodeAttributeBandaNodeAttributeas parameters. - softwarematteraNodeAttributeandbNodeAttributeare interchangeable (i.e. if @Mihran's answer will solve your problem), then you may as well replace them with a single attribute in the base class. - Oliver Charlesworth