Holding an instance of a typical type - C ++

I have a class tree_nodeand tree.

template<typename T>
class tree_node
{
public:
   tree_node(const std::string& key_, const T& value_) 
        : key(key_), value(value_)
   {
   }
private:
   T value;
   std::string key;
};

template<typename T>
class tree
{
public:
   tree() : root(new tree_node<T>("", ???)) { }
private:
   tree_node<T>* root;
};

tree_nodeexpects an instance Tat creation. How to transfer it to the field ???? I can say T(), but it will only work if it Thas a constructor without parameters. I cannot have a constructor without parameters for tree_node, since it will not compile if it Tdoes not have a constructor without parameters.

I am looking for a development method tree_nodethat can contain all types correctly, including pointer types.

Edit

, boost::optional. T value boost::optional<T> value. . tree_node, key. node. ?

..

+3
4

Init root . node, , , .

template<typename T> 
class tree 
{ 
public: 
   tree() : root(0) { } 
   void push (const std::string& key, const T & t) {
      if (root == 0) {
        root = new tree_node<T>(key, t);
      } else {
         // Make complex tree
      }
   }
private: 
   tree_node<T>* root; 
}; 

, :

enum NodeType { EMPTY_NODE, VALUE_NODE };

class base_tree_node 
{ 
public: 
   base_tree_node() :parent(0), left(0), right(0) {}

   virtual NodeType gettype() = 0;

protected: 
   base_tree_node* parent;
   base_tree_node* left;
   base_tree_node* right;
}; 

class empty_tree_node : base_tree_node
{
   virtual NodeType gettype()  { return EMPTY_NODE; }
}

template<typename T> 
class tree_node : base_tree_node 
{ 
public: 
   tree_node(const std::string& key_, const T& value_)  
        : key(key_), value(value_) 
   { 
   } 

   virtual NodeType gettype()  { return VALUE_NODE; }

private: 
   T value; 
   std::string key; 
}; 
+4
tree( const T & t ) : root(new tree_node<T>("", t )) { }
+3

- ( ), node, - , :

struct BaseNode
{
    BaseNode* next;
    BaseNode(BaseNode* next): next(next) {}
};

template <class T>
struct Node: public BaseNode
{
    T data;
    Node(const T& data, BaseNode* next): BaseNode(next), data(data) {}
};

template <class T>
struct List
{
    BaseNode* head;
    List(): head(new BaseNode(0)) {}
    void add(const T& value)
    {
        Node<T>* new_node = new Node<T>(value, head->next);
        head->next = new_node;
    }
    T& get_first()
    {
        assert(head->next);
        return static_cast<Node<T>*>(head->next)->data;
    }
    //...
};

The class itself must make sure that it receives the necessary casts and does not try to pour its head or root on Node<T>.

0
source

The node tree must have (or be) a set of child nodes. The tree must have (or be) a collection of root nodes. Both of these collections must be of the same type. Very simple:

template <class T>
class NodeCollection
{
    std::vector<Node<T> *> nodes;

public:
    // any operations on collection of nodes
    // copy ctor and destructor a must!
};

template <class T>
class Node : public NodeCollection<T> 
{
    T value;

public:
    // ctor
    // access to value
};

template <class T>
class Tree : public NodeCollection<T> 
{
public:
    // ctor
};

Thus, the general definition of Treeand is Nodeactually in NodeCollection, and therefore, it is Treenot necessary to carry a fictitious value.

0
source

All Articles