Sep. 13th, 2011

yigal_s: (Default)
Вот тот код, который позволяет доступаться до приватных членов класса.
http://bloglitb.blogspot.com/2010/07/access-to-private-members-thats-easy.html
  1. template<typename Tag>
  2. struct result {
  3.   /* export it ... */
  4.   typedef typename Tag::type type;
  5.   static type ptr;
  6. };
  7.  
  8. template<typename Tag>
  9. typename result<Tag>::type result<Tag>::ptr;
  10.  
  11. template<typename Tag, typename Tag::type p>
  12. struct rob : result<Tag> {
  13.   /* fill it ... */
  14.   struct filler {
  15.     filler() { result<Tag>::ptr = p; }
  16.   };
  17.   static filler filler_obj;
  18. };
  19.  
  20. template<typename Tag, typename Tag::type p>
  21. typename rob<Tag, p>::filler rob<Tag, p>::filler_obj;
  22.  
  23. struct A {
  24. private:
  25.   void f() {
  26.     std::cout << "proof!" << std::endl;
  27.   }
  28. };
  29.  
  30. struct Af { typedef void(A::*type)(); };
  31. template class rob<Af, &A::f>;
  32.  
  33.  
  34. int main() {
  35.   A a;
  36.   (a.*result<Af>::ptr)();
  37. }