title: How to return a smart pointer to a member variable?
body: <p>I'm attempting to create an accessor for a class member variable using smart pointers. Here's the code:</p>
  
  <pre><code>class MyResource
  {
  };
  
  class MyClass
  {
  public:
      std::unique_ptr&lt;MyResource&gt; getResource();
  private:
      std::unique_ptr&lt;MyResource&gt; resource;
  };
  
  std::unique_ptr&lt;MyResource&gt; MyClass::getResource()
  {
      return this-&gt;resource;
  }
  </code></pre>
  
  <p>The error I get trying to compile this:</p>
  
  <blockquote>
    <p>cannot access private member declared in class 'std::unique_ptr&lt;_Ty>'</p>
  </blockquote>
  
  <p>Adding <code>.get</code> to <code>this-&gt;resource</code> of course doesn't work because the return type changes.</p>
  
  <p>Should I not be using a unique_ptr here? Is this just a syntax issue? Am I totally on the wrong track?</p>
  
  <p><strong>my background with smart pointers:</strong>
  I've been using plain-old-pointers for a couple of years now in part because I can't find a solid explanation of when to use which types of smart pointers and how to go about using them. I'm tired of making excuses, so I'm just diving in. I think I understand what smart pointers are and why to use them, but I understand very little of the details. At the moment I'm totally lost in <a href="https://stackoverflow.com/questions/1119962/smart-pointers-when-where-and-how">the</a> <a href="https://stackoverflow.com/questions/8242320/smart-pointers-why-use-them-and-which-to-use">endless</a> <a href="https://stackoverflow.com/questions/417481/pointers-smart-pointers-or-shared-pointers">Q&amp;A</a> <a href="https://stackoverflow.com/questions/106508/what-is-a-smart-pointer-and-when-should-i-use-one">about</a> <a href="https://stackoverflow.com/questions/6876751/differences-between-unique-ptr-and-shared-ptr">smart</a> <a href="https://stackoverflow.com/questions/5576922/pimpl-shared-ptr-or-unique-ptr">pointers</a>.</p>