title: Safely override C++ virtual functions
body: <p>I have a base class with a virtual function and I want to override that function in a derived class. Is there some way to make the compiler check if the function I declared in the derived class actually overrides a function in the base class? I would like to add some macro or something that ensures that I didn't accidentally declare a new function, instead of overriding the old one.</p>
  
  <p>Take this example:</p>
  
  <pre><code>class parent {
  public:
    virtual void handle_event(int something) const {
      // boring default code
    }
  };
  
  class child : public parent {
  public:
    virtual void handle_event(int something) {
      // new exciting code
    }
  };
  
  int main() {
    parent *p = new child();
    p-&gt;handle_event(1);
  }
  </code></pre>
  
  <p>Here <code>parent::handle_event()</code> is called instead of <code>child::handle_event()</code>, because the child's method misses the <code>const</code> declaration and therefore declares a new method. This could also be a typo in the function name or some minor difference in the parameters types. It can also easily happen if the interface of the base class changes and somewhere some derived class wasn't updated to reflect the change.</p>
  
  <p>Is there some way to avoid this problem, can I somehow tell the compiler or some other tool to check this for me? Any helpful compiler flags (preferably for g++)? How do you avoid these problems?</p>