如何处理参数名称重影方法?

时间:2019-03-12 09:02:14

标签: c++ gcc

下面的代码是使用gcc 4.9和-std = c ++ 14 -Wshadow编译的,产生以下警告:

main.cpp:12:24: warning: declaration of ‘id’ shadows a member of 'this' [-Wshadow]

main.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文件中完成操作,以免丑陋到达公共标头。还有其他想法吗?

0 个答案:

没有答案
相关问题