title: Using smart pointers in a struct or class
body: <p>I've written a function that loads the bytes off a file and returns a FileData struct that contains the byte buffer and the length of the buffer.</p>
  
  <p>I want the buffer to be deleted as soon as it's consumed and thrown out of scope.</p>
  
  <p>I'm having trouble getting it to compile due to various casting errors. Also, I'm not sure whether the buffer is being moved correctly rather than copied. I don't mind the FileData struct itself being copied, since it's maybe 16 bytes at most.</p>
  
  <p>In general, how do you use smart pointers as class/struct fields? Is that even something you'd do?</p>
  
  <p>This is a bit of a nebulous question, I know, but since I'm having some conceptual difficulties with smart pointers in general, I'm hoping that this example will help me in the right direction.</p>
  
  <p>Here's what I've got so far:</p>
  
  <pre><code>struct FileData
  {
      unique_ptr&lt;char[]&gt; buf;
      unsigned int len;
  };
  
  FileData LoadFile(string filename)
  {
      ifstream str;
      str.open(filename, ios::binary);
  
      str.seekg(0, ios::end);
      auto len = str.tellg();
      str.seekg(0, ios::beg);
  
      char* buf = new char[len];
  
      str.read(buf, len);
      str.close();
  
      FileData d = { unique_ptr&lt;char[]&gt;(buf), len };
  
      return d;
  }
  </code></pre>
  
  <p><strong>Edit:</strong> Since some people are curious about the error message that I get with this current code, here it is:</p>
  
  <pre><code>error C2248: 'std::unique_ptr&lt;_Ty&gt;::unique_ptr' : cannot access private member declared in class 'std::unique_ptr&lt;_Ty&gt;'
  </code></pre>