Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-3719

Virtual functions should not have default arguments

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Message:
      Remove this default argument initializer.
    • Highlighting:
      Hide

      = xx

      Show
      = xx
    • Default Severity:
      Critical
    • Impact:
      High
    • Likelihood:
      Low
    • Covered Languages:
      C++
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      20min
    • Analysis Scope:
      Main Sources, Test Sources

      Description

      It's best to avoid giving default argument initializers to virtual functions. While doing so is legal, the code is unlikely to be correctly maintained over time and will lead to incorrect polymorphic code and unnecessary complexity in a class hierarchy.

      Noncompliant Code Example

      class Base {
      public:
        virtual void fun(int p = 42) { // Noncompliant
          // ...
        }
      };
      
      class Derived : public Base {
      public:
        void fun(int p = 13) override { // Noncompliant
          // ...
        }
      };
      
      class Derived2 : public Base {
      public:
        void fun(int p) override {
          // ...
        }
      };
      
      int main() {
        Derived *d = new Derived;
        Base *b = d;
        b->fun(); // uses default argument 42
        d->fun(); // uses default argument 13; was that expected?
      
        Base *b2 = new Base;
        Derived2 *d2 = new Derived2;
        b2->fun(); // uses default argument 42
        d2->fun(); // compile time error; was that expected?
      }
      

      Compliant Solution

      class Base {
      public:
        void fun(int p = 42) { // non-virtual forwarding function
          fun_impl(p);
        }
      protected:
        virtual void fun_impl(int p) {
          // ...
        }
      };
      
      class Derived : public Base {
      protected:
        void fun_impl(int p) override {
          // ...
        }
      };
      
      class Derived2 : public Base {
      protected:
        void fun_impl(int p) override {
          // ...
        }
      };
      

      See also

      • S1712

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated: