#ifndef _TEST_H
#define _TEST_H
#include <iostream>
#include <string>
using namespace std;
struct test1
{
int a;
};
struct test2
{
test1 t;
int b;
int c;
};
struct test3
{
test2 t;
int d;
int e;
int f;
};
void main()
{
test1 t1;
t1.a = 1;
test2 t2;
t2.t = t1;
t2.b = 2;
t2.c = 3;
test3 t3;
t3.t = t2;
t3.d = 4;
t3.e = 5;
t3.f = 6;
test1 *pT1 = &t1;
test2 *pT2 = &t2;
test3 *pT3 = &t3;
//这里组织的三个结构体内存分布和三个类一次继承下来的内存分布
//是一样的,感觉操作上也非常相似,尤其是向上类型转换和向下类
//型转换,和类继承相比限制条件更加少了一下,尤其是向下类型转换。
//相当于test2从test1中继承,test3从test1和test2中多继承
//相当于向上类型转换
pT1 = (test1 *)pT2;
cout << pT1->a << endl;
//相当于向下类型转换
pT2 = (test2 *)pT1;
cout << pT2->b << endl;
cout << &(pT1->a) << endl;
cout << &(pT2->b) << endl;
//相当于向上类型转换
pT2 = (test2 *)pT3;
cout << pT2->t.a << endl;
cout << pT2->b << endl;
cout << pT2->c << endl;
//相当于向下类型转换
pT3 = (test3 *)pT2;
cout << pT3->d << endl;
cout << pT3->e << endl;
cout << pT3->f << endl;
}
#endif //_TEST_H</p>