#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>