C++ generate a warning when a class member shadow a class member of its parent?

前端 未结 3 1214
灰色年华
灰色年华 2021-01-17 10:21

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         


        
3条回答
  •  情书的邮戳
    2021-01-17 10:33

    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.

提交回复
热议问题