当类成员影响其父类的成员时,C会生成警告?

前端之家收集整理的这篇文章主要介绍了当类成员影响其父类的成员时,C会生成警告?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当派生类成员变量名称影响其父类之一时,是否有一种生成警告的方法,例如
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' */
};

使用-Wshadow与g编译时,上述代码不会生成警告.

解决方法

实际上我看到代码如下,显示了阴影警告的必要性.
int val = 0;

if (flag == aval) 
  int val = firstval;
else if (flag == bval)
  int val = secondval;
else if
.
.
.

switch (val)
{

// put cases here

}

我还看到阴影警告,其中内部变量意图是本地的,对外部变量没有影响,并且不应该引用阴影变量.其实,更改名称更容易,以防止警告.

原文链接:https://www.f2er.com/c/112526.html

猜你在找的C&C++相关文章