RED-local-hides-member (C++ only)
In this section:
Synopsis
The definition of a local variable hides a member of the class.
Enabled by default
No
Severity/Certainty
Medium/Medium

Full description
A local variable is declared in a class function with the same name as a member of the class, hiding the member from this scope, from this point onwards. This might be intentional, but it is better to use a different name for the variable, so that a reference to the class member does not accidentally change or return the local value.
Coding standards
- CERT DCL01-C
Do not reuse variable names in subscopes
- CERT DCL01-CPP
Do not reuse variable names in subscopes
Code examples
The following code example fails the check and will give a warning:
class A {
int x;
public:
void foo(int y) {
for(int x = 0; x < 10 ; x++){
y++;
}
}
void foo2(int y) {
int x = 0;
x+=y;
return;
}
void foo3(int y) {
{
int x = 0;
x+=y;
return;
}
}
};
The following code example passes the check and will not give a warning about this issue:
class A {
int x;
};
class B {
int y;
void foo();
};
void B::foo() {
int x;
}