title: Use of unique_ptr for class members
body: <p>I'm implementing a C++ program to read images from a TGA file. I got a struct for the header.</p>
  
  <pre><code>struct TGA_HEADER
  {
  // length of id string
  char    id_length;
  
  // image storage info
  char    colour_map_type;
  char    image_type;
  
  // colour Map
  short   first_entry;
  short   num_entries;
  char    bits_per_entry;
  
  // image description
  short   x_origin;
  short   y_origin;
  short   width;
  short   height;
  char    bits_per_pixel;
  char    descriptor;
  };
  </code></pre>
  
  <p>The image class looks like this:</p>
  
  <pre><code>class image
  {
  private:
  
  public:
  TGA_HEADER header;
  std::unique_ptr&lt;std::vector&lt;char&gt;&gt; pixel_data;
  
  image(const std::string&amp; image_path);   
  ~image();
  
  static void save_image(const std::string&amp; file_name, image&amp; image);
  static void load_image(const std::string&amp; path, const std::unique_ptr&lt;std::vector&lt;char&gt;&gt;&amp; pixel_data, TGA_HEADER&amp; header);
  }; 
  </code></pre>
  
  <p>The constructor for my image class:</p>
  
  <pre><code>image::image(const std::string&amp; image_path) 
  : 
  pixel_data(new std::vector&lt;char&gt;)
  {
  load_image(image_path, this-&gt;pixel_data, this-&gt;header);
  }
  </code></pre>
  
  <p>And my loading class:</p>
  
  <pre><code>void image::load_image(const std::string&amp; path, const std::unique_ptr&lt;std::vector&lt;char&gt;&gt;&amp; pixel_data, TGA_HEADER&amp; header)
  {
  std::ifstream file_stream(path, std::ios::in | std::ios::binary | std::ios::ate);
  
  if (file_stream.is_open())
  {
      file_stream.seekg(0, std::ios::beg);
  
      int tgaDesc = 0;
  
      /* read header */
      file_stream.read(&amp;header.id_length, sizeof(header.id_length));
      file_stream.read(&amp;header.colour_map_type, sizeof(header.colour_map_type));
      file_stream.read(&amp;header.image_type, sizeof(header.image_type));
  
      file_stream.read((char*)(&amp;header.first_entry), sizeof(header.first_entry));
      file_stream.read((char*)(&amp;header.num_entries), sizeof(header.num_entries));
      file_stream.read(&amp;header.bits_per_entry, sizeof(header.bits_per_entry));
  
      file_stream.read((char*)(&amp;header.x_origin), sizeof(header.x_origin));
      file_stream.read((char*)(&amp;header.y_origin), sizeof(header.y_origin));
      file_stream.read((char*)(&amp;header.width), sizeof(header.width));
      file_stream.read((char*)(&amp;header.height), sizeof(header.height));
      file_stream.read(&amp;header.bits_per_pixel, sizeof(header.bits_per_pixel));
      file_stream.read(&amp;header.descriptor, sizeof(header.descriptor));
  
      // Skip the ID String
      char* skip = new char[256];
      file_stream.read(skip, header.id_length);
  
      // Skip the colour map if it doesn't exist
      if (!(tgaDesc &amp; 4))
      {
          int colourMapSize = header.colour_map_type * header.num_entries;
          file_stream.read(skip, colourMapSize);
      }
  
      delete skip;
  
      int imageDataSize = header.width * header.height * (header.bits_per_pixel / 8);
      pixel_data-&gt;resize(imageDataSize);
  
      int originalPosition = (int)file_stream.tellg();
  
      /* read image data */
      file_stream.read(pixel_data-&gt;data(), imageDataSize);
  }
  </code></pre>
  
  <p>My first question is, if I'm using the <code>unique_ptr</code> for my pixel_data in a correct way, especially in the constructor, and my second question would be if I initalize the <code>TGA_HEADER</code> object correctly respectively if I have to delete it manually or use smart pointers.</p>
  
  <p>I'm quite new to C++, so feel free to comment on other issues you might come across.</p>
  
  <p>Best regards</p>