title: How to check validity of a shared pointer in a class?
body: <p>Why checking the validity of root results in segmentation fault and what is the correct way of checking if the pointer is null?</p>
  
  <pre><code>class Node {  
  public:
    int key, value;
    std::shared_ptr&lt;Node&gt; right, left;
    Node(int key, int value) : key(key), value(value) {
      right = nullptr;
      left = nullptr;
    }
  };
  
  class BST {
  private:
    std::shared_ptr&lt;Node&gt; root = nullptr;
  public:
    void addElement(int key, int value) {  
      if (root) {
      }
    }
  };
  
  int main() {
    std::shared_ptr&lt;BST&gt; bst;
    bst-&gt;addElement(10, 10);
  }
  </code></pre>