title: Using C++ construction initializer to open/read file
body: <p>I wrote a simple C++ class program to read and/or to print out some BMP header information as shown below and it works just fine. However, I want to take the advantage of what a C++ contructor initializer can offer. So, I want to use the C++ contructor initializer to open the file as well as retrieve the data, i.e. mOffBits, mWidth, and mHeight. Is this possible?</p>
  
  <p>Header file:</p>
  
  <pre><code>#include &lt;fstream&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  class BMP
  {
    public:
          BMP (const std::string&amp; inFileName);
          ~BMP ();
          const void printInfo () const;
  
    private:
          std::ifstream ifs;
          std::string mInFileName;
          std::uint32_t mOffBits;
          std::int32_t mWidth, mHeight;
          char str [54];
  };
  </code></pre>
  
  <p>Program file:</p>
  
  <pre><code>#include "bmp.h"
  
  BMP::BMP (const std::string&amp; inFileName) : mInFileName (inFileName)
  {
          ifs.open (mInFileName, std::ios::in | std::ios::binary);
  
          ifs.seekg (0, std::ios::beg);
          ifs.read (&amp;str [0], 54);
          mOffBits = *reinterpret_cast &lt;std::uint32_t*&gt; (&amp;str [0xA]);
          mWidth = *reinterpret_cast &lt;std::int32_t*&gt; (&amp;str [0x12]);
          mHeight = *reinterpret_cast &lt;std::int32_t*&gt; (&amp;str [0x16]);
  }
  
  
  BMP::~BMP ()
  {
          ifs.close ();
  }
  
  
  const void BMP::printInfo () const
  {
          std::cout &lt;&lt; "\tInput filename:\t\t" &lt;&lt; mInFileName &lt;&lt; std::endl;
          std::cout &lt;&lt; "\tBegin of IMG data:\t" &lt;&lt; mOffBits &lt;&lt; std::endl;
          std::cout &lt;&lt; "\tHeight:\t\t\t" &lt;&lt; mHeight &lt;&lt; std::endl;
          std::cout &lt;&lt; "\tWidth:\t\t\t" &lt;&lt; mWidth &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>main.cpp:</p>
  
  <pre><code>#include "bmp.h"
  
  auto main (int argc, char* argv []) -&gt; int
  {
          if (argc &lt; 2)
          {
                  std::cout &lt;&lt; argv [0] &lt;&lt; " image.bmp" &lt;&lt; std::endl;
                  return 1;
          }
  
          // Itialize bmp class;
          BMP bmp (argv [1]);
  
          // print out header information.
          bmp.printInfo ();
  
          return 0;
  }
  </code></pre>