classObject{ public: int data_0 = 1; int data_1 = 2; int data_2 = 3; int data_3 = 4; };
连续的内存,只要能知道data_0的内存地址,后面三个偏移一下也就得到了。
1 2 3 4 5 6 7 8 9 10 11
intmain(){
Object obj; int *readO = (int *)&obj; std::cout << "obj地址:" << &obj << std::endl; for (int i = 0; i < sizeof(obj); i++){ std::cout << "内存地址: " << &readO[i] << "值:" << readO[i] << std::endl; }
return0; }
通过直接打印查看
可以看到int类型的内存占用之间相差四字节。
并且类的内存分配跟private和public那些没啥关系,当然前提是正常的类。
进行第一次派生
1 2 3 4 5 6 7
classson :public Object{ public: int sdata_0 = 11; int sdata_1 = 12; int sdata_2 = 13; int sdata_3 = 14; };
看到son类继承了Object的属性 可以说是把Object的成员照搬了过来
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classson :public Object{ /* public: int data_0 = 1; int data_1 = 2; int data_2 = 3; int data_3 = 4; */ public: int sdata_0 = 11; int sdata_1 = 12; int sdata_2 = 13; int sdata_3 = 14; };
第二次派生
1 2 3 4 5 6 7
classgrandson :public son{ public: int gsdata_0 = 21; int gsdata_1 = 22; int gsdata_2 = 23; int gsdata_3 = 24; };