5

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;
    }
9
  • How about color = color... Nov 28, 2013 at 21:09
  • 1
    @BitFiddlingCodeMonkey No! That won't work as long parameter and member have exactly the same name! Nov 28, 2013 at 21:11
  • We have a macro at work like this: #define TOUCH(v) (v = v)... Nov 28, 2013 at 21:11
  • An init function like this is just terrible, use the constructor. Also, if this happens, you need to rethink your interface. If you're not using the parameter, how much sense does it make to call that function with that parameter? If it's not used on one end, how would you fill it in on the other? Anyways, a car has a color, don't be so silly. If the base class has this member, the constructor damn well initialize it too. Otherwise get rid of it.
    – rubenvb
    Nov 28, 2013 at 21:14
  • 2
    @BitFiddlingCodeMonkey Does this really not emit any code? How about side-effects for operator=? What happens for non-assignable types? What I have often seen is (void)color; or #define TOUCH(x) while(0) { (void)x; }. This does not emit any code, works for every type, and is used in many code bases.
    – pmr
    Nov 28, 2013 at 21:15

3 Answers 3

11

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

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

You can omit the name of the function parameter.

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

This is sometimes not desirable because some tools use the signature to extract documentation and you define the function inline. Then you can use a cast.

struct car {
  void init(int color, int size) {
    (void)color; // prevent warning
    this->size = size;
  }
};

At the same time, please remember that C++ classes often don't need init functions because that's what constructors are for.

1

There no need to name function parameters: if you don't use a parameter, just leave its name off:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.