-3

How can do I implement the SearchTree constructor with the T type parameter by calling it's superclass ?

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;    
}
Agnaroc
  • 167
  • 2
  • 10
  • 10
    don't derive from std::unique_ptr. That way madness lies. encapsulate it. – Richard Hodges May 14 '16 at 13:28
  • It's a school exercise, this code is given. I need to implement the constructor ... – Agnaroc May 14 '16 at 13:29
  • 3
    You can't just write a copy constructor; you have to know what the proper semantics are. `unique_ptr` itself doesn't have a copy constructor, because it doesn't make sense to have multiple copies of something that's supposed to be unique. It has a move constructor, so that only one `unique_ptr` object holds a pointer to the managed resource. So the first thing you have to do is answer the threshold question: what does it mean to copy a `SearchTree` object? – Pete Becker May 14 '16 at 13:32
  • Teacher probably want's you to implement deep copy. – Zereges May 14 '16 at 13:39
  • Can't I just create a new unique_pointer ? Since SearchTree derives from unique_ptr > SearchTree is also an unique pointer ? So when I call the constructor can't I create a new one ? – Agnaroc May 14 '16 at 13:39
  • @Agnaroc You can't separate the child from its parent. Your child class is SuperClass. When you create a SuperClass, the unique_ptr is "built-in" as the parent. – PaulMcKenzie May 14 '16 at 13:43
  • @PaulMcKenzie so there is no way I can create a SearchTree object and assign a value to the sl variable ? – Agnaroc May 14 '16 at 13:45
  • @Zereges - well, yes, obviously, it's about a deep copy. But that doesn't answer the question of **what** a deep copy means when it involves a `unique_ptr` that, by it's nature, is **never** copied. – Pete Becker May 14 '16 at 14:02

1 Answers1

1

Inheriting from std::unique_ptr is an instant indicator of a design flaw.

Encapsulation is the way to go. Perhaps start with something like this?

#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);

}
Richard Hodges
  • 68,278
  • 7
  • 90
  • 142