Re: C2248: cannot access protected member
Ben Voigt [C++ MVP] <rbv@nospam.nospam> wrote:
"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:OUIS1rEZIHA.4684@TK2MSFTNGP06.phx.gbl...
I guess both of you meant
void (Y::*pf)() = &Y::foo;
No, I meant what I wrote
void (Y::*pf)() = &X::foo;
That still shouldn't compile, due to access check on X::foo, as
described in C++ standard 11.5/1. See my earlier answer in this thread.
I don't know if the C++ compiler will accept that syntax, it should
because it is type safe.
The problem has nothing to do with type safety, but with access control.
Y can access the base implementation of protected member foo, so it
should be able to create a pointer-to-member that does so.
If it were able to do so, it would be able to call foo on some other
class Z derived from X. But it shouldn't be able to call a protected
method on an unrelated class, even if the two share the same base.
--
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