Advertisement
Xeeynamo

Private attribute access

Jul 12th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.57 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. class A
  4. {
  5. private:
  6.     int a, b, c;
  7. public:
  8.     A()
  9.     {
  10.         a = 1;
  11.         b = 2;
  12.         c = 3;
  13.     }
  14.     void Print()
  15.     {
  16.         printf("%i %i %i\n", a, b, c);
  17.     }
  18. };
  19.  
  20. struct APrivate
  21. {
  22.     int* a;
  23.     int* b;
  24.     int* c;
  25.  
  26.     APrivate(A* _this)
  27.     {
  28.         a = ((int*)_this) + 0;
  29.         b = ((int*)_this) + 1;
  30.         c = ((int*)_this) + 2;
  31.     }
  32. };
  33.  
  34. int main()
  35. {
  36.     A a;
  37.     APrivate ap(&a);
  38.  
  39.     // Before
  40.     a.Print();
  41.  
  42.     // After
  43.     *ap.a = 4;
  44.     *ap.b = 5;
  45.     *ap.c = 6;
  46.     a.Print();
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement