By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,795 Members | 1,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,795 IT Pros & Developers. It's quick & easy.

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

P: n/a
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 -

Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Wed, 03 Dec 2003 11:29:19 GMT, "Risto Lankinen"
<rl******@hotmail.com> 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().

Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.