title: Does this C++ code acutally have a dangling pointer?
body: <p>I have class <code>B</code>, which contains a pointer for object of class <code>A</code>. I want them to share their position, so I used <code>std::shared_ptr</code> for it. Below is a short version of this code:</p>
  <pre class="lang-cpp prettyprint-override"><code>struct A {
      std::shared_ptr&lt;int&gt; pos;
  
      A(const A&amp; right, std::shared_ptr&lt;int&gt; _pos = nullptr) :
          pos(_pos == nullptr ? (std::make_shared&lt;int&gt;()) : _pos) { }
  };
  
  struct B {
      std::shared_ptr&lt;int&gt; pos;
      A* a;
      
      B(const B&amp; right) :
          pos(std::make_shared&lt;int&gt;()), 
          a(new A(*right.a, pos)) { }
  };
  </code></pre>
  <p>In the actual program it seems to work fine, however Visual Studio gives warning <code>C26815 for dangling pointer</code> in the <code>B</code> copy constructor, when giving value to <code>a</code>. Removing <code>pos</code> from <code>B</code> doesn't affect the warning, but if it's also removed from <code>A</code> the warning disappears, so I guess it have something to do with <code>shared_ptr</code>? Is there a dangling pointer here, or it's a false warning?</p>