加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 03:06:38 所属栏目:百科 来源:网络整理
导读:当派生类成员变量名称影响其父类之一时,是否有一种生成警告的方法,例如 class Mother {public: Mother() : i(0) {} virtual ~Mother() {}protected: int i;};class Child : public Mother{public: Child() : Mother(),i(0) {} virtual ~Child() {}protected:
当派生类成员变量名称影响其父类之一时,是否有一种生成警告的方法,例如
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

}

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读