473,398 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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

Sep 9 '05 #1
6 8553
"Andre Eisenbach" <in*****@gmail.com> wrote in message
news:11********************@f14g2000cwb.googlegrou ps.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

Sep 9 '05 #2
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

Sep 9 '05 #3
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

Sep 9 '05 #4

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

Sep 9 '05 #5
in*****@gmail.com 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

Sep 9 '05 #6
<in*****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.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

Sep 11 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Scott Brady Drummonds | last post by:
Hi, everyone, I'm still learning Python as I develop a medium-sized project. From my previous experience with C++, I've burnt into my mind the notion of information hiding. I'm having trouble...
8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
4
by: Marc Schellens | last post by:
If I have a base class with a public virtual member function, a child class will grand access to its overridden memeber function even if its private in the child class, as the compliler cannot...
12
by: Bryan Parkoff | last post by:
CMain Class is the base class that is initialized in main function. CA Class is the base class that is initialized in CMain::CMain(). CMain Class is always public while CA Class is always...
4
by: Justin Miller | last post by:
Ok, I tried to make that subject as descriptive as possible. What I'm trying to do: I'm attempting to use policies to create a generic memento (design pattern) template. My Memento template so...
6
by: zfareed | last post by:
Is it possible to print an array that is declared from a class type that also contains private data members? I have declared an array in the main function and then set the values for the array...
7
by: PengYu.UT | last post by:
Hi, I want to write a test function to test a class. The class has a private member function that need to be tested. Although I could modify the member function as protected or public, I do not...
11
by: Yarco | last post by:
For example: <?php class Test { private $name = 'yarco'; } $p = new ReflectionPropery('Test', 'name'); print $p->getValue();
6
by: WaterWalk | last post by:
I find friend declaration just very tricky. I tried the following examples on both MingW(gcc 3.4.2) and VC++ 2005. The results are surprising. Example1: namespace ns1 { class Test { friend...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.