As we can set the private variable of class like this
I was trying to set the private member variable int for below case.
#include <iostream>
#include <string>
using namespace std;
class Test
{
private:
string s;
int data;
public:
Test() : s("New") , data(0) { }
int getData() { return data; }
};
int main()
{
Test t;
int* ptr = (int*)&t;
*(ptr+sizeof(string)) = 10;
cout << t.getData();
return 0;
}
But it is not able to print 10.
I know there are other way to set this using setter function, but was checking to set using method shown here
This is purely hack and not valid way though to learn.