c++ - C++ 警告:使用未使用的参数覆盖虚函数
问题描述
这是我在 StackOverflow 上发布的第一个问题。我遇到了要解决的 C++ 警告。问题是我们在派生类中重写了虚函数,而在被重写的函数体中没有使用一些函数参数。结果,编译器不断发出警告。在我的具有类层次结构的 C++ 项目中,这是一个很常见的问题。我尝试了以下两种解决方案,两者都可以。只是想知道哪个更黄金或更广泛使用。或者如果这个问题有任何其他的黄金解决方案,请随时提出建议。
void f(int a) {} (警告) ==> void f(int a) { (void)a; }(无警告) void f(int a) {} (警告) ==> void f(int) {} (没有警告)
提前致谢!
解决方案
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).
推荐阅读
- xcode - 升级到 macOS Catalina 后,运行 XCUITest 将不断出现“正在验证“AAA”...”弹出窗口
- c++ - 缓存 std::array 的选定元素并在 C++ 中自动保持一致
- c - 在 C 中运行用户定义的函数时出现“与局部变量相关的堆栈内存地址”错误
- python - matplotlib:在每条迹线与右侧 y 轴相交的地方放置图例标签
- android - 原生 *.so 库及其标头在 android 项目中的位置?
- javascript - 按日期顺序对nodejs中的数组进行排序(从最近到最旧)
- linux - 使用 HUGETBL 映射文件的 Linux 内存
- c - 为什么在 C 中的这个 realloc 命令之后 valgrind 输出无效读取?
- database - 将数据从 Azure Blob 存储复制到 Azure SQLDB 时,Azure 数据工厂错误地复制单元格值
- c# - Unity:如何使两个具有固定关节的不同对象发生碰撞?