开发者

Fixng unused formal parameter warnings without making an abstract class

I have a base class and it has virtual functions. The issue is that, the base is allowed to be instanced, but that means that all of its functions need a definition. This causes the compiler to warn me about unused parameters. What can I do to properly get rid开发者_StackOverflow of these warnings without making pure virtual functions and making it an abstract class?

example:

class Foo {
public:
virtual void bar(int x) {} //unused formal parameter int x
}

Thanks


Usual solution is:

virtual void bar(int x) { (void)x; }

or:

virtual void bar(int) {}

This prevents the compiler from whinging. Note that this technique isn't restricted to virtual member functions; it should work for any function where you don't intend to use one or more arguments.

On an unrelated note; an instantiable base class with empty member-function definitions doesn't sound like a very good idea; are you sure that you wouldn't be better off just making it abstract?


Is there a problem in using the absolutely most obvious solution?

class Foo {
public:
  virtual void bar(int) {}
};

Just remove the parameter name.


For what it's worth, an alternative method is:

virtual void bar(int /*x*/) {}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜