Morten V Pedersen wrote:
> Victor Bazarov wrote:
>> petschy wrote:
>>> Hello,
>>>
>>> class A
>>> {
>>> protected:
>>> class B { };
>>> }
>>>
>>> class C : public A, public A::B
>>> {
>>> };
>>>
>>> this won't compile, gcc-3.3 and gcc-4.1 both give errors,
>>> complaining that A::B is not visible.
>>> However, since C inherits from A, it should access C's protected
>>> parts, including B, right?
>>
>> No. The names of the base classes have to be visible and accessible
>> in the scope where the class is being defined. You haven't opened
>> the 'C' class' scope yet to allow access to 'A::B'.
>
> I would like to understand this, but I'm not understanding the
> explanation - could you rephrase the explanation?
All base classes have to be visible and accessible (the compiler has
to be able to find the name and your class is supposed to have access
to it) in the scope of the class definition. IOW, the sheer fact that
you derive from 'A' does not give you access to 'A's members in the
list of the base classes of 'C'. Only *inside* 'C'. The list of the
base classes is *not* inside 'C'.
> As I see it there is no reason to derive from A::B since B is already
> a part of A right?
No, you're not allowed to derive from it because it's not accessible to
you. The compiler does not validate your *reasons*. It only validates
the correctness of the program.
> class A
> {
> protected:
> class B{};
> };
>
> class C : public A
> {
> };
>
> Then B would be a protected member of C right?
That's correct.
> No need to explicitly
> derive A::B
That's up to you. Inheritance (derivation) and membership are two
different things (they have different connotations and implications).
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
|