当派生类成员变量名称影响其父类之一时,有没有办法生成警告,例如
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' */
};
Run Code Online (Sandbox Code Playgroud)
-Wshadow使用g ++ 编译时,上面的代码不会生成警告.
小智 -5
这不会显示警告,因为这是允许的。这是可能的,因为“最多其中一个名称实际上是在该范围内定义的;其他名称仅在该范围内可见。名称解析规则决定选择哪个名称,如果有多个候选者......你确实这样做不想对编译器在替代方案之间进行选择的每种情况发出警告。” - @MSalters。