11.4 Protected member access [class.protected]
An additional access check beyond those described earlier in Clause 11 is applied when a non-static data member or non-static member function is a protected member of its naming class (11.2), As described earlier, access to a protected member is granted because the reference occurs in a friend or member of some class C. If the access is to form a pointer to member (5.3.1), the nested-name-specifier shall denote C or a class derived from C. All other accesses involve a (possibly implicit) object expression (5.2.5). In this case, the class of the object expression shall be C or a class derived from C. ( ISO/IEC 14882:2011(E) §11.4 )
[ Example:
-
// friend_protected_member_access.cpp
-
//
-
#include <iostream>
-
-
using namespace std;
-
-
-
class B
-
{
-
protected:
-
int i;
-
static int j;
-
};
-
-
class D1 : public B
-
{
-
};
-
-
class D2 : public B
-
{
-
friend void fr(B*,D1*,D2*);
-
void mem(B*,D1*);
-
-
public:
-
void memWrapper(B* pb, D1* p1)
-
{
-
mem(pb, p1);
-
}
-
};
-
-
void fr(B* pb, D1* p1, D2* p2)
-
{
-
cout<<"[void fr(B* pb, D1* p1, D2* p2)] called"<<endl;
-
//pb->i = 1; // ill-formed
-
//p1->i = 2; // ill-formed
-
p2->i = 3; // OK (access through a D2)
-
cout<<"[p2->i = 3;] p2->i ="<<p2->i<<endl;
-
-
p2->B::i = 4; // OK (access through a D2, even though
-
cout<<"[p2->B::i = 4;] p2->B::i="<<p2->B::i<<endl;
-
-
// naming class is B)
-
//int B::* pmi_B = &B::i; // ill-formed
-
int B::* pmi_B2 = &D2::i; // OK (type of &D2::i is int B::*)
-
cout<<"[int B::* pmi_B2 = &D2::i;] int B::* pmi_B2 = "<<pmi_B2<<endl;
-
-
B::j = 5; // OK (because refers to static member)
-
cout<<"[B::j = 5;] B::j="<<B::j<<endl;
-
-
D2::j = 6; // OK (because refers to static member)
-
cout<<"[D2::j = 6;] D2::j = "<<D2::j<<endl;
-
}
-
-
void D2::mem(B* pb, D1* p1)
-
{
-
cout<<"[void D2::mem(B* pb, D1* p1)] called"<<endl;
-
//pb->i = 1; // ill-formed
-
//p1->i = 2; // ill-formed
-
i = 3; // OK (access through this)
-
cout<<"[i = 3;] i="<<i<<endl;
-
-
B::i = 4; // OK (access through this, quali?cation ignored)
-
cout<<"[B::i = 4;] B::i="<<B::i<<endl;
-
-
//int B::* pmi_B = &B::i; // ill-formed
-
int B::* pmi_B2 = &D2::i; // OK
-
cout<<"[int B::* pmi_B2 = &D2::i;] int B::* pmi_B2="<<pmi_B2<<endl;
-
-
j = 5; // OK (because j refers to static member)
-
cout<<"[j = 5;] j = "<<j<<endl;
-
-
B::j = 6; // OK (because B::j refers to static member)
-
cout<<"[B::j = 6;] B::j = "<<B::j<<endl;
-
}
-
-
void g(B* pb, D1* p1, D2* p2)
-
{
-
cout<<"[void g(B* pb, D1* p1, D2* p2)] called"<<endl;
-
//pb->i = 1; // ill-formed
-
//p1->i = 2; // ill-formed
-
//p2->i = 3; // ill-formed
-
}
-
-
int B::j = 0;
-
-
int main(int argc, char* argv[])
-
{
-
D1 d1;
-
D2 d2;
-
D2 dPb;
-
B* pb = &dPb;
-
-
fr(pb, &d1, &d2);
-
d2.memWrapper(pb, &d1);
-
g(pb, &d1, &d2);
-
-
return 0;
-
}
— end example ]
[Result:
-
[void fr(B* pb, D1* p1, D2* p2)] called
-
[p2->i = 3;] p2->i =3
-
[p2->B::i = 4;] p2->B::i=4
-
[int B::* pmi_B2 = &D2::i;] int B::* pmi_B2 = 1
-
[B::j = 5;] B::j=5
-
[D2::j = 6;] D2::j = 6
-
[void D2::mem(B* pb, D1* p1)] called
-
[i = 3;] i=3
-
[B::i = 4;] B::i=4
-
[int B::* pmi_B2 = &D2::i;] int B::* pmi_B2=1
-
[j = 5;] j = 5
-
[B::j = 6;] B::j = 6
-
[void g(B* pb, D1* p1, D2* p2)] called
— end result]
阅读(920) | 评论(0) | 转发(0) |