title: C++ Multiple Inheritance + Virtual Functions (- Ambiguity) = Weird Behavior (also function pointers)
body: <p>I'm creating a couple of interfaces intended to provide access to a callback functionality. That is, inheriting from <em>interface A</em> allows a class to use callbacks of type one; <em>interface B</em> allows type two. Inheriting from both A and B allows callbacks of both types. The ultimate purpose is that classes A and B will take care of all the dirty work by just inheriting from them.</p>
  <h3>First Problem</h3>
  <p>Here's a small example that should illustrate some of the trouble I'm having:</p>
  <pre><code>class A
  {
  public:
      static void AFoo( void* inst )
      {
          ((A*)inst)-&gt;ABar( );
      }
      virtual void ABar( void ) = 0;
  };
  
  class B
  {
  public:
      static void BFoo( void* inst )
      {
          ((B*)inst)-&gt;BBar( );
      }
      virtual void BBar( void ) = 0;
  };
  
  class C : public A, public B
  {
  public:
      void ABar( void ){ cout &lt;&lt; &quot;A&quot;; };
      void BBar( void ){ cout &lt;&lt; &quot;B&quot;; };
  };
  </code></pre>
  <p>And by making the calls</p>
  <pre><code>C* c_inst = new C( );
  void (*AFoo) (void*) = C::AFoo;
  void (*BFoo) (void*) = C::BFoo;
  AFoo( (void*)c_inst );
  BFoo( (void*)c_inst );
  </code></pre>
  <p>I expect I'll get &quot;AB&quot; as output. Instead I get &quot;AA&quot;. Reversing the order of derived classes (B before A), produces &quot;BB&quot;. Why is this?</p>
  <h3>Second Problem</h3>
  <p>The actual interfaces I'm using are templated, so the code appears more like</p>
  <pre><code>template &lt;class T&gt; class A
  {
  public:
      static void AFoo( void* inst )
      {
          ((T*)inst)-&gt;ABar( );
      }
      virtual void ABar( void ) = 0;
  };
  
  template &lt;class T&gt; class B
  {
  public:
      static void BFoo( void* inst )
      {
          ((T*)inst)-&gt;BBar( );
      }
      virtual void BBar( void ) = 0;
  };
  
  class C : public A&lt;C&gt;, public B&lt;C&gt;
  {
  public:
      void ABar( void ){ cout &lt;&lt; &quot;A&quot;; };
      void BBar( void ){ cout &lt;&lt; &quot;B&quot;; };
  };
  </code></pre>
  <p>The reason for this is so A and B can do all the work, but their implementations don't need to have any knowledge of C.</p>
  <p>Now, calling with</p>
  <pre><code>C* c_inst = new C( );
  void (*AFoo) (void*) = C::AFoo;
  void (*BFoo) (void*) = C::BFoo;
  AFoo( (void*)c_inst );
  BFoo( (void*)c_inst );
  </code></pre>
  <p>produces the correct output: &quot;AB&quot;.</p>
  <p>This small example works fine here, but it doesn't always work correctly in practice. Very strange things start happening, similar to the weirdness in the first problem above. The main issue appears to be that both virtual functions (or the static functions, or something) don't always make it into C.</p>
  <p>For instance, I can successfully call C::AFoo(), but not always C::BFoo(). And this is sometimes dependent on the order in which I derive from A and B: <code>class C: public A&lt;C&gt;, public B&lt;C&gt;</code> may produce code where neither AFoo or BFoo work, while <code>class C: public B&lt;C&gt;, public A&lt;C&gt;</code> may produce code where one of them works, or maybe both.</p>
  <p>Since the classes are templated, I can remove the virtual functions in A and B. Doing so produces working code, so long as ABar and BBar exist in C of course. That's acceptable, but not desired; I'd rather know what the problem is.</p>
  <p>What possible reasons are there that the above code could cause bizarre problems?</p>
  <p>Why does the second example produce the correct output where though the first doesn't?</p>