By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,720 Members | 2,135 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,720 IT Pros & Developers. It's quick & easy.

is this inheritance construct legal?

P: n/a
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

Sep 14 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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
Sep 14 '06 #2

P: n/a
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
Sep 14 '06 #3

P: n/a
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.
>
Sep 14 '06 #4

P: n/a
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
Sep 14 '06 #5

P: n/a
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
Sep 14 '06 #6

P: n/a
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?
Sep 14 '06 #7

P: n/a
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
Sep 14 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.