code_junkie

Automatic Casts redux

Nov 11th, 2011
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. class P
  2. {
  3. public:
  4. void a( );
  5. };
  6.  
  7. class PW
  8. {
  9. public:
  10. PW( const P& p ) : p( p ) { }
  11. void b( );
  12. P& p;
  13. };
  14.  
  15. class C
  16. {
  17. public:
  18. P GetP( ) const { return p; }
  19. private:
  20. P p;
  21. };
  22.  
  23. // ...
  24. PW& p = c.GetP( ); // valid
  25. // ...
  26.  
  27. // ...
  28. p->p->a( )
  29. // ...
  30.  
  31. class P
  32. {
  33. public:
  34. void a( ) { std::cout << "a" << std::endl; }
  35. };
  36.  
  37. class PW
  38. {
  39. public:
  40. PW(P& p) : p(p) { }
  41.  
  42. void b( ) { std::cout << "b" << std::endl; }
  43.  
  44. P & operator*() { return p; }
  45. P * operator->() { return &p; }
  46.  
  47. private:
  48. P & p;
  49. };
  50.  
  51. class C
  52. {
  53. public:
  54. P & getP() { return p; }
  55.  
  56. private:
  57. P p;
  58. };
  59.  
  60.  
  61. int main()
  62. {
  63. C c;
  64. PW pw(c.getP());
  65.  
  66. (*pw).a();
  67. pw->a();
  68. pw.b();
  69.  
  70. return EXIT_SUCCESS;
  71. }
  72.  
  73. a
  74. a
  75. b
Add Comment
Please, Sign In to add comment