title: Best way to read binary data in C++ using classes without padding
body: <p>I am reading simple binary data, without pointers, using C++ classes without padding with the following code:</p>
  
  <pre><code>#include &lt;fstream&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  class Data {
      public:
      int a;
      int b;
      short int c;
      double d;
  }__attribute__((packed));
  
  int main() {
      Data myData;      
      ifstream ifs("test.bin", ios::binary);
      ifs.read((char *)&amp;myData, sizeof(myData));
      ifs.close();
  }
  </code></pre>
  
  <p>I am using this method because the data might have 20+ different formats and I want to write 20+ different classes to cover all the formats that might show up. I also read that other options include using bit-fields, pragma directives, and even the boost serialization routines (I can't because I have to use std). My question is: <strong>is this the  best way to read simple binary data using classes without padding? Do you suggest any other alternative?</strong> I would like to learn what is the safest and most widely used method out there.</p>