Menu

Suggestion for new check for missing override on destructors?

2021-10-12
2021-10-13
  • Steve Albright

    Steve Albright - 2021-10-12

    I really like the override keyword and have been burnt by adding const to methods/parameters and causing hiding issues. Things have gotten much better with that being enforced better in cppcheck and various functions getting flagged as missing overrides.

    The CLANG compiler has a good warning for inconsistent use of override and by adding override to destructors that check gets triggered and helps point out other missing overrides.

    To go along with your override improvements, flag destructors to help CLANG and others find missing overrides too. I would make this a destructorMissingOverride style or something like that so it can be suppressed if not wanted.

    The other benefit of adding override to destructors is that if someone takes virtual off the base then it will cause a compile error vs. unexpected lack of the derived destructor being called.

    In derived classes I often change

    virtual ~ExampleClass();

    to

    ~ExampleClass() override;

    which can also be combined with default if needed

    ~ExampleClass() override = default;

    partially based on this long article override on virtual destructors

     
  • Daniel Marjamäki

    I am not sure. If we add a warning about something stylistic I would like if that was considered "best practice". I saw that there is different opinions about this.

    maybe this is something that would fit better in a addon.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.