Connecting Tech Pros Worldwide Help | Site Map

Behavior of friend class in a namespace.

  #1  
Old March 23rd, 2007, 11:55 PM
Alan Johnson
Guest
 
Posts: n/a
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
  #2  
Old March 24th, 2007, 03:55 AM
red floyd
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
deriving from class in anonymous namespace Al Grant answers 3 November 7th, 2008 11:05 PM
Template friend function injection H9XLrv5oXVNvHiUI@spambox.us answers 21 July 11th, 2008 11:25 PM
Rectangle: struct or class? Steven T. Hatton answers 15 July 22nd, 2005 07:02 PM
Vector error in Visual Graeme answers 2 July 19th, 2005 06:58 PM