Chinaunix首页 | 论坛 | 博客
  • 博客访问: 774694
  • 博文数量: 239
  • 博客积分: 60
  • 博客等级: 民兵
  • 技术积分: 1045
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-22 18:25
文章分类

全部博文(239)

文章存档

2019年(9)

2018年(64)

2017年(2)

2016年(26)

2015年(30)

2014年(41)

2013年(65)

2012年(2)

分类: C/C++

2014-01-27 16:59:33

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:

点击(此处)折叠或打开

  1. // friend_protected_member_access.cpp
  2. //
  3. #include <iostream>

  4. using namespace std;


  5. class B
  6. {
  7. protected:
  8.     int i;
  9.     static int j;
  10. };

  11. class D1 : public B
  12. {
  13. };

  14. class D2 : public B
  15. {
  16.     friend void fr(B*,D1*,D2*);
  17.     void mem(B*,D1*);

  18. public:
  19.     void memWrapper(B* pb, D1* p1)
  20.     {
  21.         mem(pb, p1);
  22.     }
  23. };

  24. void fr(B* pb, D1* p1, D2* p2)
  25. {
  26.     cout<<"[void fr(B* pb, D1* p1, D2* p2)] called"<<endl;
  27.     //pb->i = 1; // ill-formed
  28.     //p1->i = 2; // ill-formed
  29.     p2->i = 3; // OK (access through a D2)
  30.     cout<<"[p2->i = 3;] p2->i ="<<p2->i<<endl;

  31.     p2->B::i = 4; // OK (access through a D2, even though
  32.     cout<<"[p2->B::i = 4;] p2->B::i="<<p2->B::i<<endl;

  33.     // naming class is B)
  34.     //int B::* pmi_B = &B::i; // ill-formed
  35.     int B::* pmi_B2 = &D2::i; // OK (type of &D2::i is int B::*)
  36.     cout<<"[int B::* pmi_B2 = &D2::i;] int B::* pmi_B2 = "<<pmi_B2<<endl;

  37.     B::j = 5; // OK (because refers to static member)
  38.     cout<<"[B::j = 5;] B::j="<<B::j<<endl;

  39.     D2::j = 6; // OK (because refers to static member)
  40.     cout<<"[D2::j = 6;] D2::j = "<<D2::j<<endl;
  41. }

  42. void D2::mem(B* pb, D1* p1)
  43. {
  44.     cout<<"[void D2::mem(B* pb, D1* p1)] called"<<endl;
  45.     //pb->i = 1; // ill-formed
  46.     //p1->i = 2; // ill-formed
  47.     i = 3; // OK (access through this)
  48.     cout<<"[i = 3;] i="<<i<<endl;

  49.     B::i = 4; // OK (access through this, quali?cation ignored)
  50.     cout<<"[B::i = 4;] B::i="<<B::i<<endl;

  51.     //int B::* pmi_B = &B::i; // ill-formed
  52.     int B::* pmi_B2 = &D2::i; // OK
  53.     cout<<"[int B::* pmi_B2 = &D2::i;] int B::* pmi_B2="<<pmi_B2<<endl;

  54.     j = 5; // OK (because j refers to static member)
  55.     cout<<"[j = 5;] j = "<<j<<endl;

  56.     B::j = 6; // OK (because B::j refers to static member)
  57.     cout<<"[B::j = 6;] B::j = "<<B::j<<endl;
  58. }

  59. void g(B* pb, D1* p1, D2* p2)
  60. {
  61.     cout<<"[void g(B* pb, D1* p1, D2* p2)] called"<<endl;
  62.     //pb->i = 1; // ill-formed
  63.     //p1->i = 2; // ill-formed
  64.     //p2->i = 3; // ill-formed
  65. }

  66. int B::j = 0;

  67. int main(int argc, char* argv[])
  68. {
  69.     D1 d1;
  70.     D2 d2;
  71.     D2 dPb;
  72.     B* pb = &dPb;

  73.     fr(pb, &d1, &d2);
  74.     d2.memWrapper(pb, &d1);
  75.     g(pb, &d1, &d2);

  76.     return 0;
  77. }
 — end example ]
[Result:

点击(此处)折叠或打开

  1. [void fr(B* pb, D1* p1, D2* p2)] called
  2. [p2->i = 3;] p2->i =3
  3. [p2->B::i = 4;] p2->B::i=4
  4. [int B::* pmi_B2 = &D2::i;] int B::* pmi_B2 = 1
  5. [B::j = 5;] B::j=5
  6. [D2::j = 6;] D2::j = 6
  7. [void D2::mem(B* pb, D1* p1)] called
  8. [i = 3;] i=3
  9. [B::i = 4;] B::i=4
  10. [int B::* pmi_B2 = &D2::i;] int B::* pmi_B2=1
  11. [j = 5;] j = 5
  12. [B::j = 6;] B::j = 6
  13. [void g(B* pb, D1* p1, D2* p2)] called
— end result]
   


阅读(920) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~