Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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.

Take this example:

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->handle_event(1); } 

Here parent::handle_event() is called instead of child::handle_event(), because the child's method misses the const 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.

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?

like image 537
sth Avatar asked Jan 30 '09 23:01

sth


People also ask

Can you override a virtual function?

Because virtual functions are called only for objects of class types, you cannot declare global or static functions as virtual . The virtual keyword can be used when declaring overriding functions in a derived class, but it is unnecessary; overrides of virtual functions are always virtual.

Do I need to override virtual functions?

It is not mandatory for the derived class to override (or re-define the virtual function), in that case, the base class version of the function is used. A class may have virtual destructor but it cannot have a virtual constructor.

Is it necessary to override a virtual method in C #?

Yes, you need to use the override keyword, otherwise the method will be hidden by the definition in the derived class.


2 Answers

Since g++ 4.7 it does understand the new C++11 override keyword:

class child : public parent {     public:       // force handle_event to override a existing function in parent       // error out if the function with the correct signature does not exist       void handle_event(int something) override; }; 
like image 178
Gunther Piez Avatar answered Sep 22 '22 14:09

Gunther Piez


Something like C#'s override keyword is not part of C++.

In gcc, -Woverloaded-virtual warns against hiding a base class virtual function with a function of the same name but a sufficiently different signature that it doesn't override it. It won't, though, protect you against failing to override a function due to mis-spelling the function name itself.

like image 43
CB Bailey Avatar answered Sep 20 '22 14:09

CB Bailey