Topic: nested class question


Author: Pete Becker <petebecker@acm.org>
Date: 1997/09/23
Raw View
Greg Janee wrote:
>
> Consider the following code:
>
> #include <iostream.h>
> class A {
> private:
>     class B {
>     public:
>         friend istream& operator>> (istream&, B*&);
>     };
> };
> istream& operator>> (istream& a, A::B*&) {
>     return a;
> }
>
> I've tried compiling this under several different compilers, and they
> all complain that, in the definition of operator>>, A::B is
> inaccessible.  Yet this would seem to be allowed by the C++ standard.  I
> ask you C++ linguists out there: what's the verdict, is this legal C++
> or not?

There are two access issues here: allowing >> to get at the private
members of B, and allowing >> to get at B's definition. Making >> a
friend of B takes care of the first one. But B is still a private member
of A, so you cannot use its name except in member functions of A. You
can make B a public member of A if that's appropriate, or you can make
operator>> a friend of A:

#include <iostream.h>
class A {
private:
    class B {
    public:
        friend istream& operator>> (istream&, B*&);
    };
    friend istream& operator>> (istream&, B*&);
};

istream& operator>> (istream& a, A::B*&) {
    return a;
}

 -- Pete
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]





Author: Greg Janee <gjanee@sb.grci.com>
Date: 1997/09/20
Raw View

Consider the following code:

#include <iostream.h>
class A {
private:
    class B {
    public:
        friend istream& operator>> (istream&, B*&);
    };
};
istream& operator>> (istream& a, A::B*&) {
    return a;
}

I've tried compiling this under several different compilers, and they
all complain that, in the definition of operator>>, A::B is
inaccessible.  Yet this would seem to be allowed by the C++ standard.
I
ask you C++ linguists out there: what's the verdict, is this legal C++
or not?

Many thanks,
-Greg

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Greg Janee                                     GRC International, Inc.
gjanee@sb.grci.com                                 5383 Hollister Ave.
                                          Santa Barbara, CA 93111-2349
                               805-964-7724 (voice) 805-967-7094 (fax)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]





Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/09/21
Raw View
Greg Janee <gjanee@sb.grci.com> writes:

>Consider the following code:
>
>#include <iostream.h>
>class A {
>private:
>    class B {
>    public:
>        friend istream& operator>> (istream&, B*&);
>    };
>};
>istream& operator>> (istream& a, A::B*&) {
>    return a;
>}
>
>I've tried compiling this under several different compilers, and they
>all complain that, in the definition of operator>>, A::B is
>inaccessible.  Yet this would seem to be allowed by the C++ standard.
>I ask you C++ linguists out there: what's the verdict, is this legal C++
>or not?

I think the compilers are correct here.  The friend declaration lets
your `operator >>' access the private members of `A::B'.  But it has no
permission to access the private members of `A', such as `A::B' itself.
If you add another friend declaration to make the operator a friend
of `A', it should be OK.

 #include <iostream.h>
 class A {
 private:
     class B {
     public:
         friend istream& operator>> (istream&, B*&);
     };
>>>>>>>>    friend istream& operator>> (istream&, B*&);
 };
 istream& operator>> (istream& a, A::B*&) {
     return a;
 }

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the
pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S.
Garp.
---
[ 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
    ]
[ FAQ:
http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:
http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu
    ]

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]





Author: fjh@cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/09/21
Raw View
Greg Janee <gjanee@sb.grci.com> writes:

>Consider the following code:
>
>#include <iostream.h>
>class A {
>private:
>    class B {
>    public:
>        friend istream& operator>> (istream&, B*&);
>    };
>};
>istream& operator>> (istream& a, A::B*&) {
>    return a;
>}
>
>I've tried compiling this under several different compilers, and they
>all complain that, in the definition of operator>>, A::B is
>inaccessible.  Yet this would seem to be allowed by the C++ standard.
>I ask you C++ linguists out there: what's the verdict, is this legal C++
>or not?

I think the compilers are correct here.  The friend declaration lets
your `operator >>' access the private members of `A::B'.  But it has no
permission to access the private members of `A', such as `A::B' itself.
If you add another friend declaration to make the operator a friend
of `A', it should be OK.

 #include <iostream.h>
 class A {
 private:
     class B {
     public:
         friend istream& operator>> (istream&, B*&);
     };
>>>>>>>>    friend istream& operator>> (istream&, B*&);
 };
 istream& operator>> (istream& a, A::B*&) {
     return a;
 }

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]