Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > is this inheritance construct legal?

Reply
Thread Tools

is this inheritance construct legal?

 
 
petschy
Guest
Posts: n/a
 
      09-14-2006
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?

B is a utility class, which is intended to be used in some of A's
descendants.
I can work the above error around, I'm just curious wheter the
compilers are right or me.

Cheers, p

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-14-2006
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'.

> B is a utility class, which is intended to be used in some of A's
> descendants.
> I can work the above error around, I'm just curious wheter the
> compilers are right or me.


The compilers are right.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Morten V Pedersen
Guest
Posts: n/a
 
      09-14-2006
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?

As I see it there is no reason to derive from A::B since B is already a
part of A right?

class A
{
protected:
class B{};
};

class C : public A
{
};

Then B would be a protected member of C right? No need to explicitly
derive A::B


>
>> B is a utility class, which is intended to be used in some of A's
>> descendants.
>> I can work the above error around, I'm just curious wheter the
>> compilers are right or me.

>
> The compilers are right.
>
> V


Victor
 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      09-14-2006
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?
>
> As I see it there is no reason to derive from A::B since B is already a
> part of A right?
>

It's not a subobject of A, it's just got it's type name defined there.

> class A
> {
> protected:
> class B{};
> };
>
> class C : public A
> {
> };
>
> Then B would be a protected member of C right? No need to explicitly
> derive A::B


B is not any kind of member of A or B.
>

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      09-14-2006
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


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      09-14-2006
Ron Natalie wrote:
> Morten V Pedersen wrote:
>> Victor Bazarov wrote:
>>> petschy wrote:
>>>> Hello,
>>>>
>>>> class A
>>>> {
>>>> protected:
>>>> class B { };
>>>> }
>>>> [...]

>
> B is not any kind of member of A or B.


'B' *is* a member of A. Moreover, 'B' *is* a member of 'A::B', as well.
(and, inside 'A', 'B' is a member of 'B'). The name 'B' is defined in
the scope of the class 'A', as well as inside its own scope. Please do
not confuse 'data member' with 'member'.

An instance of 'B' is not a member of an instance of 'A', of course.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Nate Barney
Guest
Posts: n/a
 
      09-14-2006
Victor Bazarov wrote:
>
> 'B' *is* a member of A. Moreover, 'B' *is* a member of 'A::B', as well.


So, every class is a member of itself?
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      09-14-2006
Nate Barney wrote:
> Victor Bazarov wrote:
>>
>> 'B' *is* a member of A. Moreover, 'B' *is* a member of 'A::B', as
>> well.

>
> So, every class is a member of itself?


Its name is. I am not sure about the rationale behind it, but the
name of every class is "inserted"/"injected" into its scope (3.4/3, 9/2).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Behavior of if construct in switch case defualt construct. Mukesh C Programming 4 03-26-2010 12:38 PM
Interface inheritance vs Implementation inheritance. Daniel Pitts Java 27 02-27-2008 01:37 AM
Private Inheritance and Publice Inheritance karthikbalaguru C++ 9 09-10-2007 01:05 PM
mul. inheritance & overloading operator new/delete solved by virtual base inheritance? cppsks C++ 0 10-27-2004 07:49 PM
Private access modifier and Inheritance (Inheritance implementation in Java) maxw_cc Java 1 12-21-2003 11:38 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57