0

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.

Rooky
  • 810
  • 3
  • 12
  • 20
  • What's with the `.get()`? – LogicStuff Jan 14 '17 at 19:19
  • 1
    This has nothing to do with `unique_ptr`. – Nicol Bolas Jan 14 '17 at 19:20
  • @NicolBolas This does not help any further. Maybe you can provide a more constructive comment. I tagged the question with "unique_ptr" because I thought it is also related to them, because the casting has to be between unique_ptrs. The ".get()" stuff were just some experiments. It did not work. – Rooky Jan 14 '17 at 19:26

0 Answers0