最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - Why isn't GCC's -Wshadow flag being raised for member variable shadowing in inheritance for this code? - S

programmeradmin0浏览0评论

I'm trying to understand member shadowing in inheritance.

Consider this code with inheritance where both classes have a public data member named x.

class A {
    public:
        int x;
};

class B : public A {
    public:
        int x; 
};

int main(void)
{
    B test;
    return 0;
}

When I compile this with : c++ -Wshadow file.cpp I don't get any warnings. This confuses me because my understanding is that shadowing occurs when an inner scope declares a name that's already declared in an outer scope.

In this case, B::x seems to be shadowing A::x, and the shadowing is real because to access A::x in B, I would need to use scope resolution: test.A::x

What am I missing about how -Wshadow works with inheritance? Is this not considered shadowing, or is there something special about member variables in inheritance?

I'm trying to understand member shadowing in inheritance.

Consider this code with inheritance where both classes have a public data member named x.

class A {
    public:
        int x;
};

class B : public A {
    public:
        int x; 
};

int main(void)
{
    B test;
    return 0;
}

When I compile this with : c++ -Wshadow file.cpp I don't get any warnings. This confuses me because my understanding is that shadowing occurs when an inner scope declares a name that's already declared in an outer scope.

In this case, B::x seems to be shadowing A::x, and the shadowing is real because to access A::x in B, I would need to use scope resolution: test.A::x

What am I missing about how -Wshadow works with inheritance? Is this not considered shadowing, or is there something special about member variables in inheritance?

Share Improve this question edited Jan 21 at 16:17 TylerH 21.1k77 gold badges79 silver badges112 bronze badges asked Jan 21 at 11:42 Anas JawadAnas Jawad 653 bronze badges 6
  • 2 This is opinion-based, since any settings related to compiler warnings are discretionary (quality of implementation concerns, about which the C++ standard says nothing) and work in a way that the implementers of that compiler decide. AFAIK, none of us can read the minds of anyone, including compiler developers – Peter Commented Jan 21 at 12:26
  • Aah I see, so my understanding is not wrong then, because I thought that maybe my understanding of shadowing is not fully correct or that I'm missing something. thanks for the clarification tho – Anas Jawad Commented Jan 21 at 13:01
  • @Peter This is a question, not an opinion. The reason is conformation to the C++ Standard. – Rud48 Commented Jan 21 at 14:40
  • @Rud48 the "why" in the title is opinion-based. Though, basing it on opinions requires to know that there is no other reason, and thats what the actual question (and answer) is about. Imho there is no need to close this – 463035818_is_not_an_ai Commented Jan 21 at 14:54
  • 2 "Shadowing" is not a technical term defined by the C++ standard; it means whatever a compiler-writer or a commentator thinks it means. – Pete Becker Commented Jan 21 at 15:17
 |  Show 1 more comment

1 Answer 1

Reset to default 7

-Wshadow warns about local variables:

Warn whenever a local variable or type declaration shadows another variable, parameter, type, class member (in C++), or instance variable (in Objective-C) or whenever a built-in function is shadowed. Note that in C++, the compiler warns if a local variable shadows an explicit typedef, but not if it shadows a struct/class/enum. If this warning is enabled, it includes also all instances of local shadowing. This means that -Wno-shadow=local and -Wno-shadow=compatible-local are ignored when -Wshadow is used. Same as -Wshadow=global.

For example:

class A {
    public:
        int x = 0;
};

class B : public A { 
    void foo() {
        int x;  // warning: declaration of 'x' shadows a member of 'B'
    }    
};

In your example x is not a local variable.

The documentation does not mention warning about member variables that shadow a member from a base.

There is no deeper reason to this other than this is how GCC implements the warning. Note that e.g., Clang does also not warn for my example (see here).

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论