当为指针类型时,将指针直接+1即可指向指针的下一个地址,例如p,p+1指向p的下一个地址。当指针类型转换为int类型时,需要+4(32位系统指针占用4个字节)可以指向指针的下一个地址,例如p,(int)p + 4指向p的下一个地址。
```cpp
#include<iostream>
using namespace std;
class Father {
public:
public:
int x = 200;
int y = 300;
};
int main(void) {
Father father;
int * vptr = (int*)&father; //对象的地址
cout << "对象的地址:" << endl;
cout << hex << vptr << endl;
cout << "第一个数据成员的地址:" << endl;
cout << &father.x << endl;
cout << vptr + 1 << endl;
cout << (int)&father + 4 << endl; //必须加4
cout << "第二个数据成员的地址:" << endl;
cout << &father.y << endl;
cout << vptr + 2 << endl;
cout << (int)&father + 8 << endl; //第二个必须加8
cout << "第一个数据成员的值:" << endl;
cout << dec << father.x << endl;
cout << *(vptr + 1) << endl;
cout << *(int *)((int)&father + 4) << endl;
cout << "第二个数据成员的值:" << endl;
cout << father.y << endl;
cout << *(vptr + 2) << endl;
cout << *(int *)((int)& father + 8) << endl;
system("pause");
return 0;
}