Safely override C++ virtual functions

112,490

Solution 1

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;
};

Solution 2

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.

Solution 3

As far as I know, can't you just make it abstract?

class parent {
public:
  virtual void handle_event(int something) const = 0 {
    // boring default code
  }
};

I thought I read on www.parashift.com that you can actually implement an abstract method. Which makes sense to me personally, the only thing it does is force subclasses to implement it, no one said anything about it not being allowed to have an implementation itself.

Solution 4

In MSVC, you can use the CLR override keyword even if you're not compiling for CLR.

In g++, there's no direct way of enforcing that in all cases; other people have given good answers on how to catch signature differences using -Woverloaded-virtual. In a future version, someone might add syntax like __attribute__ ((override)) or the equivalent using the C++0x syntax.

Solution 5

In MSVC++ you can use keyword override

class child : public parent {
public:
  virtual void handle_event(int something) <b>override</b> {
    // new exciting code
  }
};

override works both for native and CLR code in MSVC++.

Share:
112,490

Related videos on Youtube

sth
Author by

sth

Useful: LEO English &lt;-&gt; German dictionary

Updated on June 13, 2021

Comments

  • sth
    sth about 3 years

    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?

    • Akash Mankar
      Akash Mankar over 9 years
      Great question, I have been trying to figure out why my child class function doesn;t get called since an hour now!
  • strager
    strager over 15 years
    Only now have I realized this works on more than just destructors! Great find.
  • Michael Burr
    Michael Burr over 15 years
    There are a couple potential drawbacks to this: 1) the other thing that marking one or more methods as abstract does is make the base class non-instantiable, which may be a problem if that's not part of the intended use of the class. 2) the base class might not be yours to modify in the first place.
  • Steve Rowe
    Steve Rowe over 15 years
    It is if you happen to be using Visual C++
  • CB Bailey
    CB Bailey over 15 years
    Using Visual C++ does not make override a keyword in C++; it might mean that you are using something that can compile some invalid C++ source code, though. ;)
  • Jon
    Jon about 14 years
    The fact that override is invalid C++ means the standard is wrong, and not Visual C++
  • CB Bailey
    CB Bailey about 14 years
    @Jon: I didn't say that Visual C++ was wrong, I said that it might compile code that isn't C++. Visual C++ supports extensions. override is a valid identifier in C++, but even if it wasn't I'm not sure how that would make the standard wrong.
  • Jon
    Jon about 14 years
    @Charles: What I'm trying to say is that standard C++ is lacking. override is such a useful and easy to implement concept, that the standard should have it, and people using VC++ without needing to port to other compilers should use it.
  • CB Bailey
    CB Bailey about 14 years
    @Jon: OK, now I see what you were driving at. Personally I could take or leave C# style override functionality; I've rarely had problems with failed overrides and they've been relatively easy to diagnose and fix. One think I won't agree with is that VC++ users should use it. I'd prefer that C++ look like C++ on all platforms even if one particular project doesn't need to be portable. It's worth noting that C++0x will have [[base_check]], [[override]] and [[hiding]] attributes so you can opt in to override checking if desired.
  • Brian
    Brian over 12 years
    I agree with Michael Burr. Making the base-class abstract isn't part of the question. It's perfectly reasonable to have a base class with functionality in a virtual method, that you want a derived class to override. And it's just as reasonable to want to protect against another programmer renaming the function in the base class, and causing your derived class to no longer override it. The Microsoft "override" extension is invaluable in this case. I'd love to see it added to the standard, because unfortunately there's no good way to do this without it.
  • David Rodríguez - dribeas
    David Rodríguez - dribeas over 12 years
    Funnily enough, a couple of years after the comment using VC++ does not make override a keyword it seems it did. Well, not a proper keyword but a special identifier in C++11. Microsoft pushed hard enough to make a special case of this and to follow the general format for attributes and override made it into the standard :)
  • David Heffernan
    David Heffernan about 12 years
    @david in the end, override went in as a full-blown keyword rather than an attribute.
  • CB Bailey
    CB Bailey about 12 years
    @DavidHeffernan: override isn't a keyword in c++, it's an identifier with a special meaning in some contexts.
  • David Heffernan
    David Heffernan about 12 years
    @charles ok, I don't know the nuances. I just read Herb's post about attributes. Thanks.
  • David Rodríguez - dribeas
    David Rodríguez - dribeas about 12 years
    @DavidHeffernan: Charles is right, the distinction is that you can use override as a identifier everywhere else (which you could not if the identifier was a full blown keyword)
  • David Heffernan
    David Heffernan about 12 years
    Makes you wonder why it was ever considered as an attribute if it could be done this way.
  • h9uest
    h9uest over 9 years
    @hirschhornsalz: I found that when you implement the handle_event function and you append override at the end of the function implementation, g++ gives an error; if you provide an inline function implementation in the class declaration following the override keyword, everything is fine. Why?
  • Gunther Piez
    Gunther Piez over 9 years
    @h9uest override must be used in the definition. An inline implementation is both definition and implementation, so thats ok.
  • h9uest
    h9uest over 9 years
    @hirschhornsalz yeah I got the same error msg by g++. A side note, though: both you and g++ error msg used the term "class definition" - shouldn't we use "declaration"({declaration, definition} pair)? You made yourself clear in this particular context by saying "definition & implementation", but I just wonder why the c++ community suddenly decides to change terms on classes?
  • Narcolessico
    Narcolessico over 6 years
    This also prevents the base method (say BaseClass::method()) to be called in the derived implementation (say DerivedClass::method()), e.g. for a default value.