成员名称不应被覆盖
6.1.4 ID_hideMember
成员函数内的局部名称与成员名称相同会干扰阅读,易引起误解。
示例:
class A {
int i = 0; // Member object ‘i’
public:
int foo() {
int i = 0; // Non-compliant, hides the member ‘i’
return bar(i);
}
int bar(int i) { // Non-compliant, hides the member ‘i’
return i + i; // Which ‘i’ is used?
}
};
建议成员对象遵循统一的命名约定,如以“_”结尾或以“m_”开头,可有效规避这类问题:
class A {
int i_ = 0; // Member object ‘i_’
public:
int foo() {
int i = 0; // Compliant
return bar(i);
}
int bar(int i) { // Compliant
return i_ + i;
}
};
参考
CWE-1109
MISRA C 2004 5.2
MISRA C 2012 5.3
MISRA C++ 2008 2-10-2