-3

スーパークラスを呼び出して、T 型パラメーターを使用して SearchTree コンストラクターを実装するにはどうすればよいですか?

template <class T>
class SearchTree: protected unique_ptr<Node<T> >{
    public:   
        SearchTree<T>();
        SearchTree<T>(const T &); //How do I implement this ?
}

template <class T>
class Node{
    friend class SearchTree<T>;    
    public:
        Node<T>();
        Node<T>(const T & sl_):sl(sl_){};   
    private:
        const T sl;
        SearchTree<T> left,right;    
}
4

1 に答える 1

1

からの継承std::unique_ptrは、設計上の欠陥の即時の指標です。

カプセル化は進むべき道です。おそらく、このようなものから始めますか?

#include <memory>

template<class T> struct Node;

template<class T>
void add_node(std::unique_ptr<Node<T>>& next, T t);

template<class T>
  struct Node
  {
    Node(T t) : _value(std::move(t)) {}
    void add(T t)
    {
      if (t < _value) {
        add_node(_left, std::move(t));
      }
      else if(t > _value) {
        add_node(_right, std::move(t));
      }
      else {
        // what?
      }
    }


    T _value;
    std::unique_ptr<Node<T>> _left, _right;
  };

template<class T>
    void add_node(std::unique_ptr<Node<T>>& next, T t)
    {
      if (next) {
        next->add(std::move(t));
      }
      else {
        next = std::make_unique<Node<T>>(std::move(t));
      }
    }


template<class T>
  struct SearchTree
  {

    void add(T t) {
      add_node(_root, std::move(t));
    }

    std::unique_ptr<Node<T>> _root;
  };

int main()
{
  SearchTree<int> tree;
  tree.add(5);
  tree.add(3);
  tree.add(4);

}
于 2016-05-14T13:45:02.397 に答える