In the following code I found here:
http://bloglitb.blogspot.com/2010/07/access-to-private-members-thats-easy.html
it seems to go right by the C ++ private access specifier. This allows me to call private functions and read / write personal data.
A SO search found this related issue that was confirmed by a GCC compiler error
C ++ - pattern seems to violate access specifiers
So naturally, I tried using this test code for this guy. Interestingly, my gcc 4.5 compiler really has this error (it takes code and prints personal information), despite the fact that it is reported in gcc 4.3 and to me using 4.5.
Anyway, I then went to the Comeau online compiler, and some of the responses to the stream said they tried. I confirmed that Como does not accept code from this question, but it accepts my code below.
So, ultimately, my question is: did I come across an error in the GCC compiler and Comeau C ++? Will it compile with VC ++? If this is not a mistake, can someone explain how this works? I get that it can declare a static pointer to a member function and point it to the private section, but how is this done?
Miscellaneous: Yes, I know that this is actually very bad. This will also work if you declare ptr element data and allow read / write personal data. Some of the strange comments were from me, trying to make this clear. I do not call this code, and I do not take responsibility for it. I just found it on google. I may not have enough reputation points to respond to comments, but I will read everything that you say. Thanks for watching.
#include <iostream> using namespace std; //-------------------------------------------- // template<typename Tag> struct result { /* export it ... */ typedef typename Tag::type type; static type ptr; }; // allocate space for the static member template<typename Tag> typename result<Tag>::type result<Tag>::ptr; //-------------------------------------------- template<typename Tag, typename Tag::type p> struct rob : result<Tag> { /* fill it ... */ struct filler { filler() { result<Tag>::ptr = p; } }; static filler filler_obj; }; // allocate space for the static member template<typename Tag, typename Tag::type p> typename rob<Tag, p>::filler rob<Tag, p>::filler_obj; //-------------------------------------------- struct A { private: void f() { cout << "hey, don't touch me I'm private!" << endl; } }; struct Af { typedef void(A::*type)(); }; template class rob<Af, &A::f>; int main() { A a; (a.*result<Af>::ptr)(); }
~> ./a.out hey don't touch me I'm closed!
~> g ++ --version g ++ (SUSE Linux) 4.5.0 20100604 [gcc-4_5-branch revision 160292] Copyright (C) 2010 Free Software Foundation, Inc.