Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #79093

Re: Injected class name

From Bo Persson <bo@bo-persson.se>
Newsgroups comp.lang.c++
Subject Re: Injected class name
Date 2021-04-19 20:05 +0200
Message-ID <ie5v3oF8lk9U1@mid.individual.net> (permalink)
References <s5k6tc$cm$1@dont-email.me>

Show all headers | View raw


On 2021-04-19 at 17:16, Andrey Tarasevich wrote:
> Hello
> 
> A simple example
> 
>   class A
>   {
>   };
> 
>   class B : A
>   {
>     A *p;       // 1 - OK
>   };
> 
>   class C : B
>   {
>     A *p;       // 2 - Error
>   };
> 
> The declaration at point 2 is invalid, since it refers to injected class 
> name `A` in `A`. That name is inaccessible in `C`.
> 
> But what about point 1? Does it also refer to injected class name `A` in 
> `A`? If so, it shouldn't compile either, since innards of `A` are 
> inaccessible in `B`.
> 
> Yet, it compiles. Apparently, when `A` is a direct base, unqualified 
> name lookup finds a different access path to `A` (not the injected class 
> name).
> 
> What exactly does it find? Where is it described in the standard? Is it 
> something as simple as "base class list is also searched during 
> unqualified name lookup"?
> 

Yes.  :-)

My compiler says:

'A' not accessible because 'B' uses 'private' to inherit from 'A'

So in B the base class is visible, and accessible as a private part.

In C it is visible, but not accessible - as it is a private member of B.


You can change that by making the inheritance public:

class B : public A
{
};

Or you can change C to refer to the global name A, instead of to the 
private base.

  class C : B
  {
    ::A *p;
  };

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Injected class name Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-04-19 08:16 -0700
  Re: Injected class name Bo Persson <bo@bo-persson.se> - 2021-04-19 20:05 +0200
    Re: Injected class name Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-04-19 14:21 -0700
      Re: Injected class name Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-04-20 18:06 -0700
        Re: Injected class name Christian Hanné <the.hanne@gmail.com> - 2021-04-21 03:35 +0200
          Re: Injected class name Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-04-21 06:29 -0700
            Re: Injected class name Richard Damon <Richard@Damon-Family.org> - 2021-04-21 09:54 -0400
              Re: Injected class name Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-04-21 07:35 -0700
                Re: Injected class name Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-04-21 07:48 -0700
  Re: Injected class name Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-04-19 11:34 -0700
  Re: Injected class name "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-21 15:45 -0700

csiph-web