Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is it OK to specify a _private_ member function as a friend?

Reply
Thread Tools

Is it OK to specify a _private_ member function as a friend?

 
 
Risto Lankinen
Guest
Posts: n/a
 
      12-03-2003
Hi!

Should this compile:

class C
{
private:
void f();
};

class D
{
friend void C::f(); // <- "line 10"
};

int main()
{
return 0;
}

It compiles fine with many compilers, but one compiler rejects
it with the following error:

"test.cpp", line 10: Error: #265-D: function "C::f" is inaccessible

The explanation provided by the error message borders with
reasonable (since C::f after all is a private function, and hence
should be inaddressable by anyone else). Now I'm in doubt
whether this is a bug in said compiler, or if it is an exception
in the access control mechanism. I'm leaning towards a bug
in this compiler since so many others accept it, but then again,
a million flies can't be wrong...

What does the standard say, anyway?

Cheers!

- Risto -



 
Reply With Quote
 
 
 
 
Dan W.
Guest
Posts: n/a
 
      12-03-2003
On Wed, 03 Dec 2003 11:29:19 GMT, "Risto Lankinen"
<(E-Mail Removed)> wrote:

>Hi!
>
>Should this compile:
>
>class C
>{
>private:
> void f();
>};
>
>class D
>{
> friend void C::f(); // <- "line 10"
>};
>
>int main()
>{
> return 0;
>}
>
>It compiles fine with many compilers, but one compiler rejects
>it with the following error:
>
>"test.cpp", line 10: Error: #265-D: function "C::f" is inaccessible
>
>The explanation provided by the error message borders with
>reasonable (since C::f after all is a private function, and hence
>should be inaddressable by anyone else). Now I'm in doubt
>whether this is a bug in said compiler, or if it is an exception
>in the access control mechanism. I'm leaning towards a bug
>in this compiler since so many others accept it, but then again,
>a million flies can't be wrong...
>
>What does the standard say, anyway?
>
>Cheers!
>
> - Risto -
>
>

When you declare anything "friend", it means that you're giving it
permission to access you, NOT giving yourself permission to access it.
If you want class D to access a private function of class C, class C
must declare class D friend, or, preferably, just the function of
class D that needs to call C::f().

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to a member function of a class - must specify class? Matthew C++ 3 02-08-2009 12:02 AM
About partially specify template member function PengYu.UT@gmail.com C++ 3 04-05-2006 10:40 AM
Function pointer member variable to non-member function Alex C++ 0 10-15-2003 05:26 PM
Function pointer member variable to non-member function slide_o_mix C++ 0 10-15-2003 03:37 PM
Passing a pointer to member function as a parameter to another member function Newsgroup - Ann C++ 5 07-30-2003 02:54 AM



Advertisments