private friend member function - should this compile or not?

  • Thread starter Andre Eisenbach
  • Start date
A

Andre Eisenbach

Should this code compile or not?

class A
{
void f();
};

class B
{
friend void A::f();
};

I have conflicting results with different compilers and would
appreciate any export answers and c++ standard references.

Thanks,
Andre
 
D

David White

Andre Eisenbach said:
Should this code compile or not?
Yes.

class A
{
void f();
};

class B
{
friend void A::f();
};

I have conflicting results with different compilers and would
appreciate any export answers and c++ standard references.

11.4 Friends
....
4 When a friend declaration refers to an overloaded name or operator, only
the function specified by the parameter types becomes a friend. A member
function of a class X can be a friend of a class Y. [Example:
class Y {
friend char* X::foo(int);
// ...
};
-end example]

BTW, I don't see any connection between the first and second sentences of
11.4-4 except that the example happens to kill both birds with one stone.

DW
 
I

int2str

Thank you David for your quick and competent reply!

Some more discussion poitns below.

David said:

Thanks, that's what I had thought - good to see I'm not the only one
:).
The majority of compilers does so as well. A new compiler I am testing
however does not. It gives an error saying that A::f() is private and
cannot be accessed.
11.4 Friends
...
A member function of a class X can be a friend of a class Y.
...

Here I am having trouble interpreting the standard however.

The question I essentially have is:
"Can a PRIVATE member function of a class X be a friend of class Y?"

Your answer and my assumption are "Yes!", but it seems the standard is
ambiguous, no?

Thanks,
Andre
 
G

Greg

Andre said:
Should this code compile or not?

class A
{
void f();
};

class B
{
friend void A::f();
};

I have conflicting results with different compilers and would
appreciate any export answers and c++ standard references.

Thanks,
Andre

No, the code should not compile. The problem is that class B cannot
declare A::f() as a friend because A::f is inaccessible to B. A
function declared as a friend must be accessible to the class making
the declaration.

In contrast, this code:

class A
{
friend class B;

void f();
};

class B
{
friend A::f();
};

will compile because class B can now access A::f() since A has declared
B its friend.

Greg
 
I

int2str

Greg said:
No, the code should not compile. ...
... A function declared as a friend must be accessible to the
class making the declaration.

Is there any part of the C++ standard or any other documentation that
backs up this statement?

Thanks,
Andre
 
G

Greg

Is there any part of the C++ standard or any other documentation that
backs up this statement?

Thanks,
Andre

How about 11.4.7 from the Standard:

A name nominated by a friend declaration shall be accessible in
the scope of the class containing the friend declaration.

Greg
 
D

David White

Is there any part of the C++ standard or any other documentation that
backs up this statement?

11.1
1 A member of a class can be
- private; that is, its name can be used only by members and friends of the
class in which it is declared.
- protected; ...
....
4 Access control is applied uniformly to all names, whether the names are
referred to from declarations or
expressions. [Note: access control applies to names nominated by friend
declarations (11.4) and using declarations (7.3.3). ]

Sorry for my mistaken earlier post. I thought you were only asking if
nominating a class member function as a friend is allowed. The private
member access didn't register with me before or after VC++ 6.0 happily
compiled it

DW
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top