title: C++: Test if class instance implements a virtual method
body: <p>I have a base class for objects which can implement some methods:</p>
  
  <pre><code>class Base {
      //default dummy methods
      virtual int methodFoo() { return 0; }
      virtual int methodBar() { return 0; }
  };
  
  class MyObject: public Base {
      int methodFoo() { /* do something */ }
  };
  </code></pre>
  
  <p>When I have an instance of a class derived from <code>Base</code>, I need to know whether it implements <code>methodFoo</code> or uses <code>Base::methodFoo</code>:</p>
  
  <pre><code>void testMethodFoo(Base &amp;obj) {
      if(obj.methodFoo == Base::methodFoo) {
          printf("obj does not implement methodFoo\n");
      }
      else {
          printf("obj implements methodFoo\n");
      }
  }
  </code></pre>
  
  <p>Unfortunately a simple compare like this doesn't work ("invalid use of member function"). I haven't found any way to do this.</p>
  
  <p>The reason I want to do this is that <code>Base</code> represents an object that will be exposed to Lua, and <code>methodFoo</code> and <code>methodBar</code> are metamethods (basically callbacks that Lua can call when it wants to perform various operations on that object). I only want to register <code>obj.methodFoo</code> in Lua if <code>obj</code> actually implements it. I don't want to register <code>Base::methodFoo</code>, since Lua will behave differently depending if a metamethod is registered or not, and recreating that behaviour within <code>methodFoo</code> might be difficult (and potentially slow, if that operation is being invoked often).</p>