473,473 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

is this inheritance construct legal?

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
7 1239
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Ioannis Vranos | last post by:
May someone explain why does this compile? class HiddenSealBaseClass { public: HiddenSealBaseClass() { } }; class Sealed: virtual HiddenSealBaseClass
3
by: ashaniray | last post by:
Hi, The ISO-C++ spec says that the order of construction for C++ objects is: **************************************************************** .... Initialization shall proceed in the...
31
by: John W. Kennedy | last post by:
I quite understand about prototypes and not having classes as such, but I happen to have a problem involving blatant is-a relationships, such that inheritance is the bloody obvious way to go. I can...
1
by: vinoraja | last post by:
There are a number of reasons we don't implement Multiple Implementation Inheritance directly. (As you know, we support Multiple Interface Inheritance). However, I should point out that it's...
1
by: junw2000 | last post by:
When we use a derived-type object to initialize or assign a base object, if the base class has private members, how can these members be initialized, since private members can not be inherited? ...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
4
by: Joe | last post by:
We are working on a project that requires 3rd parties (ie, consultants) to use our ASP.net webforms in a very reusable manner. The big catch is that we are not allowed to give them source. There...
9
by: moleskyca1 | last post by:
I've ran into a problem with inheritance and STL containers. The code is too much to list, but here is the basic problem: #include <queue> using namespace std; class Base { public:
23
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.