开发者

Functions that do nothing and compiling with Level 4 warnings

Sometimes you have a class that is designed to be inherited, but not a开发者_如何学编程ll its functions are expected to be used all the time. Therefore, to make for more readable and easier to maintain derived classes, the inheritable functions are defined, but are empty (instead of being pure virtual). An example from RakNet

    virtual void OnDirectSocketReceive(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress) {(void) data; (void) bitsUsed; (void) remoteSystemAddress;}

Now in most cases the function has some arguments and on Level 4 warnings, it gives the warning the function arguments were not used. I am currently following RakNet's example above and doing nothing with the data. Is that the standard/common way of getting rid of the warning? Can this design be avoided altogether? Any general thoughts/insight/suggestions?


Another way is to not name the parameters. That shows that the function has no intention to use them:

virtual void OnDirectSocketReceive(const char* /*data*/, const BitSize_t /*bitsUsed*/, SystemAddress /*remoteSystemAddress*/)
{ }


You don't have to name the arguments:

virtual void OnDirectSocketReceive(const char *, BitSize_t, SystemAddress) { }


If you compile with warning level 4 then yes, that's normal. You may ignore that specific warning, do what you do in the example or remove the variable names (we comment them just out):

virtual void OnDirectSocketReceive(
      const char * /* data */,
      const BitSize_t /* bitsUsed */,
      SystemAddress /* remoteSystemAddress */)
{ }


The compiler should shut up if the arguments have no name:

virtual void OnDirectSocketReceive(const char *, const BitSize_t, SystemAddress)
{}

(But then you might need to explain the arguments more carefully in the docs.)


There is another way: you can omit names of formal parameters:

virtual void OnDirectSocketReceive(const char *, const BitSize_t, SystemAddress ) {}


I get rid of warnings about unused parameters by commenting-out their names:

virtual void OnDirectSocketReceive(const char */*data*/, const BitSize_t /*bitsUsed*/, SystemAddress /*remoteSystemAddress*/)
{
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜