Connecting Tech Pros Worldwide Forums | Help | Site Map

Behavior of friend class in a namespace.

Alan Johnson
Guest
 
Posts: n/a
#1: Mar 23 '07
Consider the following piece of code:

// test.cpp
class B ;

namespace N
{
class A
{
friend class B ;
protected:
int i ;
} ;
}

class B
{
N::A a ;
public:
B() { a.i = 42 ; }
} ;


g++ 3.4.6 will compile this without complaint. g++ 4.1.0, however,
gives the error:
test.cpp: In constructor ‘B::B()’:
test.cpp:9: error: ‘int N::A::i’ is protected
test.cpp:17: error: within this context

If the friend declaration is changed to:
friend class ::B ;

then g++ 4.1.0 also accepts the code.

I am trying to determine which (if either) behavior is correct, but I'm
having trouble locating the relevant section of the standard. Any help
would be appreciated.

--
Alan Johnson

red floyd
Guest
 
Posts: n/a
#2: Mar 24 '07

re: Behavior of friend class in a namespace.


Alan Johnson wrote:
Quote:
Consider the following piece of code:
>
// test.cpp
class B ;
>
namespace N
{
class A
{
friend class B ;
protected:
int i ;
} ;
}
>
class B
{
N::A a ;
public:
B() { a.i = 42 ; }
} ;
>
>
g++ 3.4.6 will compile this without complaint. g++ 4.1.0, however,
gives the error:
test.cpp: In constructor ‘B::B()’:
test.cpp:9: error: ‘int N::A::i’ is protected
test.cpp:17: error: within this context
>
If the friend declaration is changed to:
friend class ::B ;
>
then g++ 4.1.0 also accepts the code.
>
I am trying to determine which (if either) behavior is correct, but I'm
having trouble locating the relevant section of the standard. Any help
would be appreciated.
>
I believe 4.1 is correct. The friend declaration in the unmodified code
declares N::B to be a friend.





Closed Thread