Re: C2248 : cannot access unused privat function
Ben Voigt [C++ MVP] <rbv@nospam.nospam> wrote:
"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:eWOX0RJeIHA.4588@TK2MSFTNGP06.phx.gbl...
Mario Semo <mario_semo@Xhotmail.com> wrote:
Note that make() is a friend of Foo but not of Bar. main() is not a
friend of either. So neither function has access to the private
copy-constructor, but both need it.
No, make IS a friend of Bar!
friend Bar make();
The friend declaration is inside Foo, not inside Bar. The fact that
the function purports to return Bar is irrelevant. Consider:
class Foo {
class Bar {
friend void friendOfBar();
Here, because Bar is nested within Foo, it has access to Foo private
members, and I think friendship grants that to friendOfBar.... so it
might be better named friendOfBarAndFoo, right?
Wrong. Friendship is not transitive. Just because Bar has access to
internals of Foo, and friendOfBar is a friend of Bar, doesn't mean that
it's also a friend of Foo. So much so that friendOfBar can't even
declare a variable of type Foo::Bar because it doesn't have access to
the name "Bar" inside of Foo. Consider:
class Foo {
class Bar {
int x;
friend void friendOfBar();
};
friend Foo::Bar& friendOfFoo();
};
Foo::Bar& friendOfFoo() {
static Foo::Bar b;
// b.x = 1; // doesn't compile
return b;
}
void friendOfBar() {
// Foo::Bar b; // doesn't compile
friendOfFoo().x = 1;
}
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925