Skip to content

Instantly share code, notes, and snippets.

@IvanIvanoff
Created March 16, 2017 09:09
Show Gist options
  • Save IvanIvanoff/8055c78456b2d11b98d34ad92825fd5b to your computer and use it in GitHub Desktop.
Save IvanIvanoff/8055c78456b2d11b98d34ad92825fd5b to your computer and use it in GitHub Desktop.
Mutate private data of a class though a pointer of a class with the same memory layout
// Example program
#include <iostream>
#include <string>
class X
{
int x; //private
int y; // private
public:
void print() const { std::cout << x << " " << y << std::endl;}
};
class Z
{
public:
int x;
int y;
};
int main()
{
X x;
Z* z;
z = (Z*)&x;
z->x = 100;
z->y = 200;
x.print();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment