获取类首地址,根据成员定义顺序去计算想要的私有成员地址。
class A{
private:
int a;
float b;
public:
A()
{
a = 1;
b = 1;
}
public:
float getB()
{
return b;
}
};
void test()
{
A a;
float* p = (float*)((int*)(&a) + 1);
*p = 100;
cout << *p << endl;
cout << a.getB() << endl;
}
输出
100
100