fork download
  1. #include <memory>
  2.  
  3. namespace restricted
  4. {
  5. struct A
  6. {
  7. virtual ~A() {}
  8. virtual int value_a() const { return a ; }
  9.  
  10. protected:
  11. int a = 0 ;
  12. virtual void value_a( int i ) { a = i ; }
  13. };
  14.  
  15. struct B : A
  16. {
  17. virtual int value_b() const { return b ; }
  18.  
  19. protected:
  20. int b = 0 ;
  21. virtual void value_b( int i ) { b = i ; }
  22. };
  23. }
  24.  
  25. namespace exposed
  26. {
  27. struct A : restricted::A
  28. {
  29. using restricted::A::value_a ;
  30. } ;
  31.  
  32. struct B : restricted::B
  33. {
  34. using restricted::B::value_a ;
  35. using restricted::B::value_b ;
  36. } ;
  37. }
  38.  
  39. int main()
  40. {
  41. auto exposed_a = std::make_shared<exposed::A>() ;
  42. int v = exposed_a->value_a() ;
  43. exposed_a->value_a(v) ; // fine, exposed
  44.  
  45. auto exposed_b = std::make_shared<exposed::B>() ;
  46. v = exposed_b->value_a() ;
  47. exposed_b->value_a(v) ; // fine, exposed
  48. v = exposed_b->value_b() ;
  49. exposed_b->value_b(v) ; // fine, exposed
  50.  
  51. std::shared_ptr<restricted::A> restricted_a(exposed_a) ; // fine, exposed::A => restricted::A
  52.  
  53. v = restricted_a->value_a() ; // fine
  54. // restricted_a->value_a(v) ; // *** error **** restricted::A::value_a(int) is protected
  55.  
  56. restricted_a = exposed_b ; // also fine, exposed::B => restricted::A
  57.  
  58. std::shared_ptr<restricted::B> restricted_b(exposed_b) ; // fine, exposed::B => restricted::B
  59.  
  60. v = restricted_b->value_a() ; // fine
  61. // restricted_b->value_a(v) ; // *** error **** restricted::B::value_a(int) is protected
  62. v = restricted_b->value_b() ; // fine
  63. // restricted_b->value_b(v) ; // *** error **** restricted::B::value_b(int) is protected
  64. }
  65.  
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
Standard output is empty