首页 > 解决方案 > C++ 警告:使用未使用的参数覆盖虚函数

问题描述

这是我在 StackOverflow 上发布的第一个问题。我遇到了要解决的 C++ 警告。问题是我们在派生类中重写了虚函数,而在被重写的函数体中没有使用一些函数参数。结果,编译器不断发出警告。在我的具有类层次结构的 C++ 项目中,这是一个很常见的问题。我尝试了以下两种解决方案,两者都可以。只是想知道哪个更黄金或更广泛使用。或者如果这个问题有任何其他的黄金解决方案,请随时提出建议。

void f(int a) {} (警告) ==> void f(int a) { (void)a; }(无警告)
void f(int a) {} (警告) ==> void f(int) {} (没有警告)

提前致谢!

标签: c++

解决方案


All three (the third being [[maybe_unused]] attribute mentioned in the comments) are fine, and choice is purely aesthetic and subjective.

(void)a is least meaningful to me personally, but it has a long tradition, and some other cases of unused names don't have the same option of leaving out the name as an argument does, so it may be the only option (in previous versions of the language where [[maybe_unused]] did not yet exist).


推荐阅读