title: Why can't you use offsetof on non-POD structures in C++?
body: <p>I was researching how to get the memory offset of a member to a class in C++ and came across this on <a href="http://en.wikipedia.org/wiki/Offsetof" rel="noreferrer">wikipedia:</a></p>
  
  <blockquote>
    <p>In C++ code, you can not use offsetof to access members of structures or classes that are not Plain Old Data Structures.</p>
  </blockquote>
  
  <p>I tried it out and it seems to work fine. </p>
  
  <pre><code>class Foo
  {
  private:
      int z;
      int func() {cout &lt;&lt; "this is just filler" &lt;&lt; endl; return 0;}
  
  public: 
      int x;
      int y;
      Foo* f;
  
      bool returnTrue() { return false; }
  };
  
  int main()
  {
      cout &lt;&lt; offsetof(Foo, x)  &lt;&lt; " " &lt;&lt; offsetof(Foo, y) &lt;&lt; " " &lt;&lt; offsetof(Foo, f);
      return 0;
  }
  </code></pre>
  
  <p>I got a few warnings, but it compiled and when run it gave reasonable output:</p>
  
  <pre><code>Laptop:test alex$ ./test
  4 8 12
  </code></pre>
  
  <p>I think I'm either misunderstanding what a POD data structure is or I'm missing some other piece of the puzzle. I don't see what the problem is. </p>