C++ generate a warning when a class member shadow a class member of its parent?
c++, gcc
Solution
I actually saw code as follows which shows the necessity of the shadow warnings.
int val = 0;
if (flag == aval)
int val = firstval;
else if (flag == bval)
int val = secondval;
else if
.
.
.
switch (val)
{
// put cases here
}
I have also seen shadow warnings where the inside variable was meant to be local, have no effect on the outside variable, and the shadowed variable was not supposed to be referenced. Actually, it was easier to just change the name to prevent the warning.
Problem
Is there a way to generate a warning when a derived class member variable name shadows one of its parents class, e.g ``` class Mother { public: Mother() : i(0) {} virtual ~Mother() {} protected: int i; }; class Child : public Mother { public: Child() : Mother(), i(0) {} virtual ~Child() {} protected: int i; /* NOK Expecting warning : declaration of 'int Child::i' shadows 'int Mother::i' */ }; ``` Above code generates no warning when compiled with `-Wshadow` with g++.