title: C++ and structs (with multiple inheritance)
body: <p>I've been looking into C++ and structs for a project I'm working on; at the moment I'm using 'chained' template structures to add in data fields in as pseudo-traits.</p>
  
  <p>Whilst it works, I think I'd prefer something like multiple inheritance as in the example below:</p>
  
  <pre><code>struct a {
      int a_data;
  }; // 'Trait' A
  
  struct b {
      int b_data;
  }; // 'Trait' B
  
  struct c : public a, public b {
      int c_data;
  }; // A composite structure with 'traits' A and B.
  
  struct d : public b {
      int d_data;
  }; // A composite structure with 'trait' B.
  </code></pre>
  
  <p>My experimental code examples show they work fine, but I'm a bit perplexed as to how its actually working when things get complex.</p>
  
  <p>For example:</p>
  
  <pre><code>b * basePtr = new c;
  cout &lt;&lt; basePtr-&gt;b_data &lt;&lt; endl;
  b * basePtr = new d;
  cout &lt;&lt; basePtr-&gt;b_data &lt;&lt; endl;
  </code></pre>
  
  <p>This works fine every time, even through function calls with the a pointer as a parameter.</p>
  
  <p>My question is how does the code know where b_data is stored in one of the derived structs? As far as I can tell, the structs still use a compacted structure with no extra data (i.e. 3 int structs only take up 12 bytes, 2 ints 8 bytes, etc). Surely it needs some sort of extra data field to say where a_data and b_data are stored in a given structure?</p>
  
  <p>It's more of a curiosity question as it all seems to work regardless, and if there are multiple implementations in use, I'll happily accept a single example. Though I do have a bit of a concern as I want to transfer the bytes behind these structs through a inter-process message queue and want to know if they'll be decoded OK on the other end (all the programs using the queue will be compiled by the same compiler and run on a single platform).</p>