Details

    • Type: Language-Specification
    • Status: Active
    • Resolution: Unresolved
    • Labels:
      None
    • Impact:
      Unknown 'null' severity
    • Likelihood:
      Unknown 'null' severity
    • Default Quality Profiles:
      Sonar way, MISRA C++ 2008 recommended

      Description

      Overriding a function just to call the overridden function from the base class without performing any other actions can be useless and misleading.

      There are cases when it is justified, because redeclaring the function allows some side effects:

      • Changing the visibility of the function in the derived class
      • Preventing the base class function from being hidden by an overload added in the derived class (a using declaration could have the same effect)
      • To resolve ambiguities in cases of multiple inheritance
      • To make an inherited function final

      This rule raises an issue when an override which is not in one of the aforementioned situation only calls the overridden function, directly forwarding its arguments.

      Noncompliant Code Example

      class Base {
      public:
        virtual void f();
      };
      
      class Derived : public Base {
      public:
        virtual void f() {
          Base::f(); // Noncompliant
        }
      };
      

      Compliant Solution

      class Base {
      public:
        virtual void f();
      };
      
      class Derived : public Base {
      };
      

      or

      class Base {
      public:
        void f();
      };
      
      class Derived : public Base {
      private: // change of visibility
        virtual void f() {
          Base::f();
        }  
      };
      

      or

      class Base {
      public:
        void f();
      };
      
      class Derived : public Base {
      public:
        void f(int i);
        void f() { // Prevents hiding by f(int)
          Base::f();
        }  
      };
      

      or

      class Base {
      public:
        virtual void f();
      };
      
      class Derived : public Base {
      public:
        void f() final { // final
          Base::f();
        }
      };
      

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            evgeny.mandrikov Evgeny Mandrikov
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated: