Safely override C++ virtual functions -


I have a base class with a virtual function and I want to override that function in a derived class. Is there any way to check the compiler if the declaration function in the derived class actually overrides a function in the base class?

Take this example:

  Take this example:  class guardian {public: virtual zero hand_events (inc. Some) const {// boring Default code}}; Class child: public guardian {public: virtual zero hand_events (int somewhat) {// new exciting code}}; Int main () {original * p = new child (); P-> Handle_event (1); }  

here is called "parent :: handle_event () instead of child :: handle_event () because the child's method It does not remember the const declaration and therefore declares a new method. This function name may also contain some minor differences in typo or parameter types. This can also be easily done if the base class interface changes and some derivative class was not updated to reflect the change.

Is there some way to avoid this problem, can I somehow tell the compiler or some other tool to check it for me? Any useful compiler flags (preferably for G ++)? How do you avoid these problems?

Since G ++ 4.7 this new C ++ 11 override The child of the class: Public guardian {public: // force handle_event to override an existing function in the original function // If the error does not work with the correct signature outside the Virtual Zero HEND_Event ( Inc. some) override is present; };


Comments