is this inheritance construct legal?

P

petschy

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
 
V

Victor Bazarov

petschy said:
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
 
M

Morten V Pedersen

Victor said:
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

The compilers are right.

V

Victor
 
R

Ron Natalie

Morten said:
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.
 
V

Victor Bazarov

Morten said:
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
 
V

Victor Bazarov

Ron said:
Morten said:
Victor said:
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
 
V

Victor Bazarov

Nate said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top