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

前端 未结 3 1145
灰色年华
灰色年华 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:48

    In my tests, gcc 7.4.0 indeed does not show a warning for this with -Wshadow (nor with other -Wshadow* flags, and current documentation does not tell about this possibility.

    However clang 6.0.0 has the option -Wshadow-field (included in -Wshadow-all) which gives on your code:

    main.cxx:43:7: warning: non-static data member 'i' of 'Child' shadows member inherited from type 'Mother' [-Wshadow-field]

    int i; /* NOK Expecting warning : declaration of 'int Child::i' shadows 'int Mother::i' */

    main.cxx:34:7: note: declared here

提交回复
热议问题