views:

190

answers:

3

I have a super class like this:

class Parent
{
public:
    virtual void Function(int param);
};

void Parent::Function(int param)
{
    std::cout << param << std::endl;
}

..and a sub-class like this:

class Child : public Parent
{
public:
    void Function(int param);
};

void Child::Function(int param)
{
    ;//Do nothing
}

When I compile the sub-class .cpp file, I get this error

warning C4100: 'param' : unreferenced formal parameter

As a practice, we used to treat warnings as errors. How to avoid the above warning?

Thanks.

+10  A: 

In C++ you don't have to give a parameter that you aren't using a name so you can just do this:

void Child::Function(int)
{
    //Do nothing
}

You may wish to keep the parameter name in the declaration in the header file by way of documentation, though. The empty statement (;) is also unnecessary.

Charles Bailey
Out of votes, but +1 anyway. I find this really handy when implementing functions from parent classes.
Duracell
@Charles..If the function is inline, is it legal to do like this? `class Child : public Parent { public: void Function(int /*param*/ = 0, int param2 = 0) { std::cout << param2 << std::endl; } };`
bdhar
You mean have a nameless parameter with a default value. I'm not 100% sure but I think that it is legal. I don't know of any reason why not in any case.
Charles Bailey
@bdhar: Note that it is dangerous to provide default parameters at different levels of the hierarchy unless your default values coincide. The problem you can get is that calling the same function from two contexts might end up calling the same final overrider with different default values: `struct base { virtual void f( int i = 0); }; struct derived : base { virtual void f( int i = 5 ); }; int main() { derived d; base d.f() /* D::f(5) */; b.f(); /* D::f(0) */ }`
David Rodríguez - dribeas
+1 because now I finally know a scenario where it makes sense to omit the name.
Space_C0wb0y
Thanks, David.. Let me take care of the conditions :)
bdhar
+3  A: 

Another technique that you can use if you want to keep the parameter name is to cast to void:

void Child::Function(int param)
{
    (void)param;   //Do nothing
}
R Samuel Klatchko
+2  A: 

As @Charles Bailey mentioned, you can skip the parameter name.

However, in certain scenarios, you need the parameter name, since in debug builds you are calling an ASSERT() on it, but on retail builds it's a nop. For those scenarios there's a handy macros (at least in VC++ :-)) UNREFERENCED_PARAMETER(), which is defined like this:

#define UNREFERENCED_PARAMETER(x) x

Note that the simple cast @R Samuel Klatchko posted also works, but I personally find it more readable if the code is explicit that this is an unreferenced parameter vs. simple unexplained cast like that.

Franci Penov
Hiding behind a macro is a good idea (you are right that it's clearer then my example). That said, you should probably add the cast to your macro or you may get a warning (on g++ 4.2.1, I get `warning: statement has no effect`)
R Samuel Klatchko
You could define ASSERT(param) for non-debug build with (void)param. Your UNREFERENCED_PARAMETER suggests that's not referenced at all. But it can be referenced -- in ASSERT.
harper