局部变量隐藏全局变量¶
ID: cpp/local-variable-hides-global-variable
Kind: problem
Security severity:
Severity: recommendation
Precision: very-high
Tags:
- maintainability
- readability
Query suites:
- cpp-security-and-quality.qls
此规则查找隐藏全局变量的局部变量或参数声明。此类声明创建了具有相同名称但作用域不同的变量。这使得难以确定表达式中实际使用的是哪个变量。
建议¶
考虑更改其中一个变量的名称,以保持它们之间的区别。
示例¶
int i = 10;
void f() {
for (int i = 0; i < 10; i++) { //the loop counter hides the global variable i
...
}
{
int i = 12; //this variable hides the global variable i
...
}
}
参考文献¶
B. Stroustrup. The C++ Programming Language Special Edition p 82. Addison Wesley. 2000.