lundi 20 août 2018

~Dtor() virtual vs override - conflicting style guides

This question is more specifically about the two slightly different suggestions from the style guides.

  1. "Explicitly annotate overrides of virtual destructors with exactly one of an override or (less frequently) final specifier. Do not use virtual when declaring an override." (Ref: https://google.github.io/styleguide/cppguide.html)

    class Base {
    public:
      virtual ~Base() {}
    };
    
    class Derived : public Base {
    public:
     ~Derived() override {} // as per Google style guide
    };
    
    
  2. "If a base class destructor is declared virtual, one should avoid declaring derived class destructors virtual or override." (Ref: https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md)

    class Base {
    public:
      virtual ~Base() {}
    };
    
    class Derived : public Base {
    public:
     ~Derived() {} // as per CppCoreGuidelines
    };
    
    

Which one is more preferred as an industry standard?

I'm inclined more towards the Google guide for two reasons:

  1. Let compiler scream at us if the ~Base() is marked non-virtual and ~Derived() trying to override.
  2. Improved readability (Do not have to open up Base header to check if ~Base() is virtual or not as the override keyword is self-explanatory)

Aucun commentaire:

Enregistrer un commentaire