title: Best way to implement struct polymorphism
body: <p>The problem I'm facing in C is that I'd like to have a series of structs that have a base member from another struct. e.g.</p>
  
  <pre><code>struct foo {
      int a;
      void (*calculate)(struct foo *);
  };
  struct bar {
      int a;
      void (*calculate)(struct foo *);
      double b;
  };
  void do_thing(struct foo *a)
  {
      a-&gt;calculate(a);
  }
  </code></pre>
  
  <p>The problem I'm facing is that the following appears to violate strict aliasing rules.</p>
  
  <pre><code>void foo_calculate(struct foo *a)
  {
      struct bar *b = (struct bar*)a;
  }
  </code></pre>
  
  <p>The only way I've come up with to do this is to create a union inside <code>struct foo</code> that contains all the structs that inherit from it. Is there an easier way to accomplish this?</p>