Search code examples
c++polymorphismunused-variables

C++ fix unused parameter warning with polymorphism


I get some compiler warnings for my program concerning unused variables, and I would like to know what is an appropriate way to fix this.

I have a function that gets inherited by the base class, and in the implementation of the function for the parent I don't use all parameters that are needed for the child; of course this leads to warnings, and as I am not an experienced programmer I am not sure what is the best way to fix those warnings.

So an minimal example would be:

In the header:

    class car{
     public:
       virtual void init(int color, int size)
     private:
       int size;
    }
    class sportscar : public car{
     public:
       virtual void init(int color, int size)
     private:
       int color;
       int size;
    }

In the source file:

    void car::init(int color, int size){
      this->size = size;
    }
    void sportscar::init(int color, int size){
      this->color = color;
      this->size = size;
    }

Solution

  • All you need to do is to not name them in the implementation:

    void car::init(int /* color */, int size){
        this->size = size;
    }