C ++通过多态性修复未使用的参数警告

use*_*299 3 c++ polymorphism unused-variables

我的程序收到有关未使用变量的编译器警告,我想知道什么是解决此问题的适当方法。

我有一个被基类继承的函数,并且在父函数的实现中,我没有使用子函数所需的所有参数。当然,这会导致警告,并且由于我不是一位经验丰富的程序员,所以我不确定解决这些警告的最佳方法是什么。

因此,一个最小的示例是:

在标题中:

    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;
    }
Run Code Online (Sandbox Code Playgroud)

在源文件中:

    void car::init(int color, int size){
      this->size = size;
    }
    void sportscar::init(int color, int size){
      this->color = color;
      this->size = size;
    }
Run Code Online (Sandbox Code Playgroud)

qua*_*ana 8

您需要做的就是不要在实现中为它们命名:

void car::init(int /* color */, int size){
    this->size = size;
}
Run Code Online (Sandbox Code Playgroud)