Re: C2248: cannot access protected member
- From: "Igor Tandetnik" <itandetnik@xxxxxxxx>
- Date: Thu, 31 Jan 2008 14:14:15 -0500
Alex Blekhman <tkfx.REMOVE@xxxxxxxxx> wrote:
class X
{
protected:
void foo() {}
int a;
};
class Y : public X
{
public:
void bar()
{
X::foo(); // OK
int b = X::a; // OK
void (X::*pf)() = &X::foo; // error C2248!
int X::*pa = &X::a; // error C2248!
}
};
All compilers emit C2248 error or equivalent. I want to understand
why this happens. Why accessing protected members is OK while
taking their pointers is an error?
The short answer is that the C++ standard says so:
11.5/1 When a friend or a member function of a derived class references
a protected nonstatic member of a base class, an access check applies in
addition to those described earlier in clause 11. Except when forming a
pointer to member (5.3.1), the access must be through a pointer to,
reference to, or object of the derived class itself (or any class
derived from that class) (5.2.5). If the access is to form a pointer to
member, the nested-name-specifier shall name the derived class (or any
class derived from that class).
So &X::foo fails the access check, but &Y::foo works.
Here's the long answer. Class Y is presumed to know how it uses
protected members of its base class X, and can access them freely. But
it doesn't necessarily know how some other class Z derived from X might
use X's protected members, and is not allowed to mess up with those.
Consider:
class Z : public X {};
void Y::baz(Z* pz) {
X::foo(); // fine - calls foo() on itself
pz->X::foo(); // doesn't compile - calls foo on Z
int b = X::a; // fine - access its own members
b = pz->X::a; // doesn't compile
}
If &X::foo worked inside Y, baz() could have worked around the
restrictions like this:
void Y::baz(Z* pz) {
void (X::*pf)() = &X::foo; // hypothetical, doesn't compile
(pz->*pf)(); // same as pz->foo()
}
--
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
.
- Follow-Ups:
- Re: C2248: cannot access protected member
- From: Doug Harrison [MVP]
- Re: C2248: cannot access protected member
- From: Alex Blekhman
- Re: C2248: cannot access protected member
- References:
- C2248: cannot access protected member
- From: Alex Blekhman
- C2248: cannot access protected member
- Prev by Date: Re: C2248: cannot access protected member
- Next by Date: Re: How to revise a charactor in string?
- Previous by thread: Re: C2248: cannot access protected member
- Next by thread: Re: C2248: cannot access protected member
- Index(es):
Relevant Pages
|
Loading