Reputation: 63
I have a base class with two classes inheriting from this, and a further two classes being inherited from these. I am required to use a binary tree in order to store data, however I do not quite understand how I would store the data in the tree. In examples I have seen, the data is simply an int data
but mine could be up to four different types.
Upvotes: 0
Views: 717
Reputation: 166
If all of your the types that you want to store in your tree are derived from the same base class, then your data member can be a pointer to that base class. This way, the pointer can point to an object of the base class or any of the derived types.
eg:
class Node
{
...
private:
Vehicle* mData;
};
and then you can do something like:
node->SetData(new Car);
otherNode->SetData(new Moped);
or whatever interface you have for it, that sets the mData pointer to point to an object of some type in your class tree derived from Vehicle.
Upvotes: 2