Topic: [g++ vs CC] Is this code illegal ?


Author: Gabriel Dos Reis <dosreis@dptmaths.ens-cachan.fr>
Date: 1997/05/10
Raw View
The code below is declared illegal by CC-4.1 whereas it is accepted by
g++-2.7.2.2. Which compiler is right ?

-- Gaby


% cat > a.C
#include <iostream.h>

class A {

    int val;

    friend class B;

public:

    class B {

    public:

        void f()
        {
            A a(10);
            cout << a.val << endl;
        }
    };

    A(int i = 0) : val(i) {}
    void g()
    {
        B b;
        b.f();
    }
};


main()
{
    A a;
    a.g();
}


% CC a.C
"a.C", line 18: Error: val is not accessible from A::B::f().
1 Error(s) detected.


--
"God never does mathematics."
 -- H. Hahn.
---
[ 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                             ]





Author: Jason Merrill <jason@cygnus.com>
Date: 1997/05/11
Raw View
>>>>> Gabriel Dos Reis <dosreis@dptmaths.ens-cachan.fr> writes:

> The code below is declared illegal by CC-4.1 whereas it is accepted by
> g++-2.7.2.2. Which compiler is right ?

CC.  This will be fixed in g++ 2.8.0.

> class A {
>     int val;
>     friend class B;

This declares that ::B is a friend of A, not A::B.  You need to say

      class B;
      friend class B;

to make A::B a friend of A.

Jason
---
[ 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
]





Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/05/11
Raw View
Gabriel Dos Reis <dosreis@dptmaths.ens-cachan.fr> writes:

>The code below is declared illegal by CC-4.1 whereas it is accepted by
>g++-2.7.2.2. Which compiler is right ?
...
>class A {
>    friend class B;

The issue here is whether this grants friendship to `::B' or to `A::B'.

I don't recall the correct answer off-hand (I think it may even
have changed at some point in the not-to-distant past).
However, I suspect that if you add a forward declaration of class B
at the appropriate scope, i.e.

 class A {
     class B;
     friend class B;

then you will have more luck.

--
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
                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
]