This question has already been answered:

How can I access the private fields of a class? It does not use accessors and friend classes.

Reported as a duplicate by Abyx , aleksandr barakin , user194374, Nick Volynkin 4 Feb '16 at 6:25 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • A friend’s declaration was hidden deeply, as well as the use of multiple inheritance. In general, the question can be closed. But you can't write like that. - Stas Litvinenko

4 answers 4

Only through the addresses of these fields.

#include <iostream> class A { private: int a; virtual int b () { return a; } }; class B { public: int a; virtual int b (); }; int main() { A obj; B* ptr = static_cast<B*>(static_cast<void*>((&obj))); ptr->a = 42; std::cout<<ptr->b()<<std::endl; return 0; } 
  • No, the call follows the field name. Somehow it is implemented through a pointer and method with the static modifier. Plus inheritance. - Stas Litvinenko
  • @ Stas Litvinenko edited the answer - andrybak
  • This method has already been found. But a little different from this case. Here I have a static method that returns a pointer to a class. By calling this method, I get access to private fields. A clear violation of the principle of encapsulation and the standard of pluses, or I misunderstand something. - Stas Litvinenko

There are 3 ways to do what you ask for.

  • Zadefayn # #define private public and connect the appropriate header file (of course, meaningless in the case of PIMPL -like schemes).
  • Directly contact the intended address of the variable (this method has already been told to you). It is bad that, depending on the presence of vfptr and vcbl in the class, the relative displacement may change, that is, the method is non-portable.
  • Use the trick with the template . The most beautiful and even in some ways elegant solution.
  • Thanks for the template! It will be necessary to get acquainted at leisure. - Stas Litvinenko

You can like this:

 class A { public: A(int _a) : a(_a) {} private: int a; }; class B { public: B(int _b) : b(_b) {} public: int b; }; int main(int argc, char *argv[]) { A a(0); ((B*)(&a))->b = 1; // Aa = 1 return 0; } 

That is, writing in parallel a complete copy of this class, making the required fields public.

  • This again comes down to addressing. See above. - Stas Litvinenko
  • Well, after all, private fields are designed so that there is no access to them besides accessors / class methods. What you are asking about is in itself a clear violation of the standards of advantages (as I understand it :). - ASten
  • The problem is that it is somehow implemented in the library at work. Rewrite this piece under the standard will not give. So I'm trying to understand how it is implemented. The person who wrote this is no longer working in that place. - Stas Litvinenko
  • "By calling this method, I get access to private fields" - explain how this happens. It would be easier if you brought the source of this case, at least part, and the place that confuses you. - ASten
  • In this case, if there is one member in the structure, you can write this: ( (int ) & a) = 5; - gammaker

Recently, I myself had a similar problem. Invented a new way. I do not know whether it will suit you or not. If this class has at least one friend (no matter what, available to you in the code or not), then you can declare your class with the name, which is the name of a friend. Example:

In the Graphics.h file:

 class Material; class Graphics { public: Material* CreateMaterial(Color color) { Material* material=new Material; material->color=color; return material; } }; 

In the Material.h file:

 class Material { friend class Graphics; private: Color color; }; 

Suppose you had such a code and you don’t touch it. And then suddenly we in the main.cpp file declare an imposter Graphics:

 #include "Material.h" class Graphics { static void ChangeMaterialColor(Material* material, Color newcolor) { material->color=newcolor; } }; 

Now we can change the color of the material with the help of such an impostor-friend. The only point is that two Graphics definitions should not be declared connected to one cpp file, otherwise they will conflict. In my example, this is observed: Material does not connect Graphics, but is content with its declaration.