We are considering a design that requires casting which does not feel right. Obviously googled the issue but have not found an answer. Would appreciate advice on how to avoid the need for casting. Here is a stripped down example (please execuse any typos, this has not been compiled).
struct NodeBase
{
enum class type
{
value,
aggregate
};
type m_type;
};
struct NodeAggregate : public NodeBase
{
std::vector<NodeBase*> m_list;
};
struct NodeValue : public NodeBase
{
std::string m_key;
std::string m_value;
};
The above classes can be used to create a tree structure with multiple levels.
The 'difficulty' is to develop an algorithm that traverses this structure without casting. The type variable in the base class shall identify the correct type and will reduce the number of cast to a single but it does not avoid casting.
What would be an alternative design for this issue?
Appreciate any comments.