I have a simple class hierarchy:
class Node { };
class InternalNode : public Node { };
class Leaf : public Node { };
void f(unordered_map<unsigned, unique_ptr<Node>>& node_table)
{
auto it_node = node_table.find(index);
unique_ptr<InternalNode> node;
if (it_node != node_table.end())
{
node = std::move(it_node->second.get()); // no viable overloaded '='
node = dynamic_cast<InternalNode*>(it_node->second.get()); // no viable conversion from 'InternalNode *' to 'unique_ptr<InternalNode>'
node = dynamic_cast<InternalNode&>(it_node->second.get()); // dynamic_cast from rvalue to reference type 'InternalNode &'
// ...
// dynamic_cast from rvalue to reference type 'InternalNode &'
} else {
node = make_unique<InternalNode>(values.at(index), colors.at(index), depth);
}
}
So I want dynamically insert and retrieve values into and from the node_table, which maps on the superclass "Node". Unfortunately no way worked so far to assign the retrieved value and cast it from "Node" to "InternalNode". At this point it is for sure an "InternalNode" and not a "Leaf". There are a few examples what I tried, there should be a more or less simple solution.