| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Andre Eisenbach" |
| Date: |
09 Sep 2005 03:10:42 AM |
| Object: |
private friend member function - should this compile or not? |
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
.
|
|
| User: "David White" |
|
| Title: Re: private friend member function - should this compile or not? |
09 Sep 2005 04:29:33 AM |
|
|
"Andre Eisenbach" <int2str@gmail.com> wrote in message
news:1126253442.454365.5910@f14g2000cwb.googlegroups.com...
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
.
|
|
|
| User: "" |
|
| Title: Re: private friend member function - should this compile or not? |
09 Sep 2005 04:43:19 AM |
|
|
Thank you David for your quick and competent reply!
Some more discussion poitns below.
David White wrote:
Should this code compile or not?
Yes.
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
.
|
|
|
|
|
| User: "Greg" |
|
| Title: Re: private friend member function - should this compile or not? |
09 Sep 2005 05:16:16 AM |
|
|
Andre Eisenbach wrote:
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
.
|
|
|
| User: "" |
|
| Title: Re: private friend member function - should this compile or not? |
09 Sep 2005 05:30:08 AM |
|
|
Greg wrote:
Andre Eisenbach wrote:
Should this code compile or not?
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
.
|
|
|
| User: "David White" |
|
| Title: Re: private friend member function - should this compile or not? |
10 Sep 2005 10:28:59 PM |
|
|
<int2str@gmail.com> wrote in message
news:1126261808.450970.181510@g43g2000cwa.googlegroups.com...
Greg wrote:
Andre Eisenbach wrote:
Should this code compile or not?
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?
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
.
|
|
|
|
| User: "Greg" |
|
| Title: Re: private friend member function - should this compile or not? |
09 Sep 2005 06:07:37 AM |
|
|
wrote:
Greg wrote:
Andre Eisenbach wrote:
Should this code compile or not?
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
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
.
|
|
|
|
|
|

|
Related Articles |
|
|