Topic: Derived nested classes in derived classes
Author: David R Tribble <dtribble@technologist.com>
Date: 1999/03/30 Raw View
Francis Glassboro wrote:
>> Should the following code compile?
>>
>> struct B {
>> protected:
>> struct Nested {};
>> };
>>
>> struct D: public B {
>> struct DNested: public B::Nested {};
>> };
>>
>> int main()
>> D d;
>> return 0;
>> }
>>
>> IOW does the ctor for DNested have access to the ctor for Nested?
Jim Hyslop <jim.hyslop@leitch.com> writes
> I'm still not convinced the Standard allows the behaviour, although I
> believe it should.
Francis Glassborow wrote:
> Which is my position. Perhaps we should ask WG21 & J16 for an
> interpretation.
Agreed.
I favor the position that since class B::Nested is protected, its
visibility is limited to classes derived from B, in the same way
that a protected typedef B::Foo would be. At the same time, the
default ctor and dtor of B::Nested are public, meaning that if a
class (such as D) can indeed access B::Nested, then that class has
public access to the ctor and dtor.
It would probably be a different matter if B::Nested::Nested() was
protected; in this case, only classes derived from B::Nested
(such as D::DNested) would have access to its ctor.
But then there is the question of whether nested classes within a
derived class have access to the protected class (without being
friends) in the first place. My intuition is that they don't -
I don't believe nested classes enjoy the same access privileges
that their outer classes do. But since D::DNested is also derived
from the protected class B::Nested, it is more than just a nested
class of D, so it does have access to members of B::Nested (and
would have access to them even if they were protected).
-- David R. Tribble, dtribble@technologist.com --
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@fermi.ceg.uiuc.edu (Siemel Naran)
Date: 1999/03/24 Raw View
On 24 Mar 99 06:18:21 GMT, Francis Glassborow
>In article <slrn7ffcfu.74t.sbnaran@localhost.localdomain>, Siemel Naran
>>Yes. The ctor and dtor of B::Nested is public, so class D and
>>nested classes of class D can see it.
>Look again, Nested is a protected member of B.
I maintain my original position. Note that class B::Nested is protected.
This means that only B, classes derived from B, and friends of B can see
B::Nested.
(In addition, the constructor of B::Nested must be public or protected
for D::Nested, which is derived from B::Nested, to see it. But this is
another matter.)
I had to add two semi-colons and a brace. Both como and egcs compile
your program.
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Jim Hyslop <jim.hyslop@leitch.com>
Date: 1999/03/24 Raw View
In article <p6q4sncxd1k.fsf@pandora.inst-inf-1.hu-berlin.de>,
Martin von Loewis <loewis@informatik.hu-berlin.de> wrote:
> Francis Glassborow <francis@robinton.demon.co.uk> writes:
>
> > Should the following code compile?
>
> I'd say it should.
>
> > IOW does the ctor for DNested have access to the ctor for Nested?
>
> Yes. The implicit special member functions (ctors, dtors, assignment
> operators) inline public, so the constructors are accessable.
Accessible by whom? Note that it is not the derived class that is trying to
access the special member functions, it is a class nested within the derived
class - do nested classes of derived classes have access to protected members
of the enclosing class's base class? Not according to 11.8/1 (or, as you
prefer, [class.access.nest]/1).
> In addition, this is explained in [class.access.nest]/2: The name
> Nested is injected publically into class Nested, therefore the
> implicit constructor of DNested will find the injected name, which is
> accessible.
Hmmm... that section says that "the base-clause may refer to the private
members of the enclosing class" and gives an example showing a derived class
within the base class. There is no mention of accessing protected members of
the enclosing class's base class.
I'm still not convinced the Standard allows the behaviour, although I believe
it should.
Jim
Note to recruitment agencies: I will not refer my friends or colleagues
to you nor do I want to use your services to find me a job. I stop
reading unsolicited email as soon as I determine it is job-recruitment
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/03/25 Raw View
In article <7db9g3$gvj$1@nnrp1.dejanews.com>, Jim Hyslop
<jim.hyslop@leitch.com> writes
>I'm still not convinced the Standard allows the behaviour, although I believe
>it should.
Which is my position. Perhaps we should ask WG21 & J16 for an
interpretation.
Francis Glassborow Chair of Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/03/23 Raw View
Should the following code compile?
struct B {
protected:
struct Nested {}
};
struct D: public B {
struct DNested: public B::Nested {}
};
int main ()
D d;
return 0;
}
IOW does the ctor for DNested have access to the ctor for Nested?
Francis Glassborow Chair of Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@localhost.localdomain.COM (Siemel Naran)
Date: 1999/03/23 Raw View
On 23 Mar 99 11:10:40 GMT, Francis Glassboro
>Should the following code compile?
>
>struct B {
>protected:
> struct Nested {}
>};
>
>struct D: public B {
> struct DNested: public B::Nested {}
>};
>
>int main ()
> D d;
> return 0;
>}
>
>IOW does the ctor for DNested have access to the ctor for Nested?
Yes. The ctor and dtor of B::Nested is public, so class D and
nested classes of class D can see it.
If the ctor of B::Nested were protected, then only classes derived
from B::Nested -- which usually means nested classes of class D
derived from B::Nested -- can see it.
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Martin von Loewis <loewis@informatik.hu-berlin.de>
Date: 1999/03/24 Raw View
Francis Glassborow <francis@robinton.demon.co.uk> writes:
> Should the following code compile?
I'd say it should.
> IOW does the ctor for DNested have access to the ctor for Nested?
Yes. The implicit special member functions (ctors, dtors, assignment
operators) inline public, so the constructors are accessable.
In addition, this is explained in [class.access.nest]/2: The name
Nested is injected publically into class Nested, therefore the
implicit constructor of DNested will find the injected name, which is
accessible.
Regards,
Martin
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Jim Hyslop <jim.hyslop@leitch.com>
Date: 1999/03/24 Raw View
In article <Iq9EcBAmM392Ew6t@robinton.demon.co.uk>,
Francis Glassborow <francisG@robinton.demon.co.uk> wrote:
> Should the following code compile?
>
> struct B {
> protected:
> struct Nested {}
> };
>
> struct D: public B {
> struct DNested: public B::Nested {}
> };
>
> int main ()
> D d;
> return 0;
> }
>
> IOW does the ctor for DNested have access to the ctor for Nested?
FWIW, I tried your code on MSVC5 and 6, and (after fixing the obvious typos)
both refused to compile complaining "cannot access protected struct declared
in class 'B'"
Intuitively, it should compile. I can see no reason to disallow it; it seems
like a natural thing to do. Question is, what does the Standard say about it?
I think 11.8/1 is key here: "The members of a nested class have no special
access to members of an enclosing class, nor to classes or functions that
have granted friendship to an enclosing class" and the reciprocal for
enclosing classes. One way of looking at this clause is that protected
members implicitly grant friendship to derived classes, so from that point of
view a derived class is a friend. Now, in order for your class heirarchy to
work, the nested member of D needs special access to a protected member of B,
but since B::Nested has not granted D::DNested friendship, access is
prohibited.
I don't like it, I don't necessarily agree with it (unless someone can
demonstrate why you should not be able to do such a thing) so I think this
should go on the list of items to consider for the next round.
This same interpretation, by the way, would also preclude containing a Nested
object from being a member of a nested class of D:
struct D : public B
{
struct Nested2 { B::Nested bn; };
};
which may or may not be a good thing; I don't know.
Jim
Note to recruitment agencies: I will not refer my friends or colleagues
to you nor do I want to use your services to find me a job. I stop
reading unsolicited email as soon as I determine it is job-recruitment
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/03/24 Raw View
In article <slrn7ffcfu.74t.sbnaran@localhost.localdomain>, Siemel Naran
<sbnaran@localhost.localdomain.COM> writes
>
>On 23 Mar 99 11:10:40 GMT, Francis Glassboro
>
>>Should the following code compile?
>>
>>struct B {
>>protected:
>> struct Nested {}
>>};
>>
>>struct D: public B {
>> struct DNested: public B::Nested {}
>>};
>>
>>int main ()
>> D d;
>> return 0;
>>}
>>
>>IOW does the ctor for DNested have access to the ctor for Nested?
>
>Yes. The ctor and dtor of B::Nested is public, so class D and
>nested classes of class D can see it.
>
>If the ctor of B::Nested were protected, then only classes derived
>from B::Nested -- which usually means nested classes of class D
>derived from B::Nested -- can see it.
Look again, Nested is a protected member of B.
>
Francis Glassborow Chair of Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]