Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old March 23rd, 2007, 11:55 PM
Alan Johnson
Guest
 
Posts: n/a
Default Behavior of friend class in a namespace.

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
Default 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.





 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,338 network members.