c++ - 如何处理参数名称阴影方法?
问题描述
下面的代码使用 gcc 4.9 和 -std=c++14 -Wshadow 编译产生以下警告:
main.cpp:12:24: warning: declaration of ‘id’ shadows a member of 'this' [-Wshadow]
主.cpp:
#include <string>
class Foo
{
public:
Foo(std::string id);
const std::string& id() const noexcept;
private:
const std::string m_id;
};
Foo::Foo(std::string id) : m_id(id) {}
const std::string& Foo::id() const noexcept { return m_id; }
int main(int argc, char** argv) { return 0; }
我继承的代码库包含很多这样的代码。原始作者选择的命名约定(我必须坚持)要求 getter 不以“get”为前缀。仅仅为了摆脱这个警告而重命名诸如id之类的参数听起来像很多工作都是徒劳的。目前我倾向于添加一个尾随下划线 - 它只需要在 .cpp 文件中完成,这样丑陋就不会到达公共标题。有人有其他想法吗?
解决方案
推荐阅读
- vba - Copy full file path to clipboard as file object so the file can be pasted in a folder in windows explorer
- azure-devops - Azure DevOps 自定义字段 - 我们可以让它们特定于团队或区域吗?
- c++ - 开发人员 C++ 中的 C++ 重命名函数总是返回“权限被拒绝”
- mongodb - 猫鼬中的 [{type: String}] 和 {type: [String]} 有什么区别?
- java - 如何通过 HarEntry java 获取请求标头
- struct - 来自子结构的父结构的 Rust 参考
- java - Jmeter:使用具有不均匀列的 CSV 文件来测试驱动采样器
- sql - Nifi为插入约束生成错误的列名
- perl - Perl脚本在godaddy共享主机上抛出内部错误
- xml - 将应用于 DTD 的规则转换为 XSD