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;
    }
  • How about `color = color`... – Fiddling Bits Nov 28 '13 at 21:09
  • 1
    @BitFiddlingCodeMonkey No! That won't work as long parameter and member have exactly the same name! – πάντα ῥεῖ Nov 28 '13 at 21:11
  • We have a macro at work like this: `#define TOUCH(v) (v = v)`... – Fiddling Bits Nov 28 '13 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 '13 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 '13 at 21:15
  • @BitFiddlingCodeMonkey To solve what exactly? That thing doesn't make any sense for me (besides I don't like macros anyway). Are you paid per LOC? Any decent compiler would optimize out this code anyway. (still nodding) ... – πάντα ῥεῖ Nov 28 '13 at 21:16
  • @pmr Just for silencing warnings in debug code. Certainly not left in production code! – Fiddling Bits Nov 28 '13 at 21:16
  • @BitFiddlingCodeMonkey pmr has made the point what's working well for a broad number of toolchains. So if you're going to compile your release software it's interlarded with loads of warnings, isn't it? You should at least change that macro definition to `#define TOUCH(v) (void)v` – πάντα ῥεῖ Nov 28 '13 at 21:20
  • @g-makulik Just telling you what I've seen... not necessarily what I've endorsed... – Fiddling Bits Nov 28 '13 at 21:22

3 Answers3

11

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

void car::init(int /* color */, int size){
    this->size = size;
}
quamrana
  • 37,849
  • 12
  • 53
  • 71
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.

pmr
  • 58,701
  • 10
  • 113
  • 156
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;
}
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380