Skip to content

Instantly share code, notes, and snippets.

@cournape
Created August 14, 2014 16:40
Show Gist options
  • Save cournape/bf17ac1186ae69150c57 to your computer and use it in GitHub Desktop.
Save cournape/bf17ac1186ae69150c57 to your computer and use it in GitHub Desktop.
privacy is dead
#include <iostream>
class Foo {
private:
int m_value;
public:
Foo(int i) : m_value(i) {};
int get_value(void) { return m_value; };
};
class Bar {
public:
int m_value;
};
int main()
{
Foo foo(1);
Bar *p_bar = reinterpret_cast<Bar*>(&foo);
std::cout << "foo value: " << (foo.get_value()) << std::endl;
// This fails
// foo.m = 2
p_bar->m_value = 3;
std::cout << "foo value: " << (foo.get_value()) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment