c++classstructtypedefusing

How can i select one of declared structure and make alias?


enum class Color { RED, BLACK };
enum class TreeModel { AVL, RED_BACK, SPLAY }

struct BasicNode {
    BasicNode* left;
    BasicNode* right;
    BasicNode* parent;
};

struct SplayNode : BasicNode {
    
}

struct RBNode : BasicNode {
    Color color;
};

struct AVLNode : BasicNode {
    unsigned int height;
};

....

???? node_;

if(condition == 1)
   node = new AVLNode;
else if(condition == 2)
   node = new RBNode;
else
   node = new SplayNode;

how to make struct type selection

i just started to learn c++ and dont know how to select struct type depending on class template param, when i construct class.


Solution

  • Ignoring all the guff around it and just answering the question you have of, what is the best way to have a variable that can contain multiple types.
    When I am in this situation; if what you have as node is a local variable, I move the rest of the code into a template (sometimes lambda) function so I can pass the type I want. If your node was a member variable I'd make it a union or varient of the types it could be. Here I would use a union as all the possible types are derived from the same base and it would be harder to access members of the base class if it was a varient.