Why does this code template violate the C ++ private access specification?

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.

+7
source share
1 answer

As Loki Astari said, public and private are just semantics for the compiler, so it can give you warnings that you are not using your code as you intended.

I don’t play with method pointers a lot (if at all), so I don’t have the patience to fully understand this. But this is no different from the fact that you make a mixture of the following. That is, using the logic of the pointer, point to any bit of memory that you want, and sketch functions and methods and abuse them.

 #include <iostream> using namespace std; struct A { public: void g() { cout << "value of i is " << this->i << endl; } void setJ(int newJ) { j = newJ; } int i; private: int j; }; int main() { A a; ai = 5; a.setJ(10); // accessing private field j cout << "value of j is " << *((&a.i)+1) << endl; // creating a pointer to method g void(A::*method)() = &A::g; // changing it to be a function pointer void(*function)(A*) = (void(*)(A*)) method; // using function pointer to call A::g function(&a); return 0; } 
+3
source

All Articles