在UNIX下面利用GCC -E -i target target.c展开,这样看代码的结构就可以忽略宏定义,直观些。对于宏定义中"##"t "#"的作用在此做一简单说明,详细可以参考GNU。
## : 将前后二个内容进行串接。
# : 将参数内的内容转换成字符串。
# 18 "object.h" 2
typedef struct Object *Object;
typedef struct ObjectClass *ObjectClass;
struct ObjectClass {
size_t __size;
ObjectClass __super;
void (*Des)(Object);
};
extern struct ObjectClass __Object;
struct Object {
ObjectClass __vptr;
};
int Object__IsKindOf(Object this, void *class);

Object Object__AllocArr(size_t size, short dim);
void Object__DeleteArr(Object obj);

void Object_NoIm(void);
# 7 "object.c" 2
# 1 "/usr/include/assert.h" 1 3 4
# 25 "/usr/include/assert.h" 3 4
void __attribute__((__cdecl__)) __assert (const char *, int, const char *);
# 9 "object.c" 2

struct ObjectClass __Object = {
sizeof(struct Object),
((void *)0),
(void (*)(Object))Object_NoIm
};
int Object__IsKindOf(Object this, void *class) {
ObjectClass c;
for (c = this->__vptr; c; c = c->__super)
if (c == class)
return 1;
return 0;
}
Object Object__AllocArr(size_t size, short dim) {
short *a = (short*)malloc(sizeof(short)+dim*size);
if (a == ((void *)0))
return ((void *)0);
*a = dim;
return (Object)(a + 1);
}
void Object__DeleteArr(Object pobj) {
short *a = (short*)pobj - 1;
size_t s = ((ObjectClass)pobj->__vptr)->__size;
char *p;
short i;
for (i = 0, p = (char*)pobj; i < *a; i++, p += s)
(*((ObjectClass)(((Object)(p))->__vptr))->Des)((Object)(p));
free((void*)a);
}
void Object_NoIm(void) {
((0) ? (void)0 : __assert("object.c", 44, "0"));
}

# 9 "string.h" 2

typedef struct String *String; struct String {struct Object super;
char *__buffer;
};
extern struct StringClass __String;
typedef struct StringClass *StringClass;
struct StringClass
{
struct ObjectClass super;
};
String StringCon1(String this, const char *str);
String StringCon2(String this, String other);

void StringDes(String this);

const char *StringToChar(String this);

# 7 "string.c" 2




struct StringClass __String;
static void StringClassCon(String t)
{
StringClass vptr=&__String;
memcpy(vptr,((Object)t)->__vptr,sizeof(struct ObjectClass));
((ObjectClass)vptr)->__size=sizeof(struct String);
((ObjectClass)vptr)->Des = (void (*)(Object))StringDes;
((ObjectClass)vptr)->__super=((Object)t)->__vptr;
}
String StringCon1(String this, const char *str)
{
{
(&this->super)->__vptr=&__Object;
};
{
if(((ObjectClass)&__String)->__size==0)
StringClassCon(this);
((Object)(this))->__vptr=(void*)&__String;
};

this->__buffer = (char*)malloc(strlen(str) + 1);
if (!this->__buffer)
return ((void *)0);
strcpy(this->__buffer, str);
return this;
}
String StringCon2(String this, String other) {
return StringCon1(this, StringToChar(other));
}
const char *StringToChar(String this) {
return this->__buffer;
}
void StringDes(String this) {
free(this->__buffer);
{};
}
# 10 "shapes.h" 2

typedef struct Scaleable **Scaleable; struct Scaleable {ptrdiff_t __offset;
void (*Scale)(Object, double);
};

typedef struct Shape *Shape;
struct Shape
{
struct Object super;
struct Scaleable *Scaleable;
struct String name;
};
extern struct ShapeClass __Shape;
typedef struct ShapeClass *ShapeClass; 
struct ShapeClass
{
struct ObjectClass super;
struct Scaleable Scaleable;
double (*Area)(Shape);
};
Shape Shape_Con(Shape this, char *name);
void Shape_Des(Shape this);


typedef struct Rect *Rect; struct Rect {struct Shape super;
double __w;
double __h;
};
extern struct RectClass __Rect;
typedef struct RectClass *RectClass;
struct RectClass {
struct ShapeClass super;
};
Rect RectCon(Rect this, char *name, double w, double h);
double RectArea(Rect this);
void RectScale(Rect this, double mag);


typedef struct Circle *Circle;
struct Circle
{
struct Shape super;
double __r;
};
extern struct CircleClass __Circle;
typedef struct CircleClass *CircleClass;
struct CircleClass
{
struct ShapeClass super;
};
Circle CircleCon(Circle this, char* name, double r);
double CircleArea(Circle this);
void CircleScale(Circle this, double mag);
# 7 "shapes.c" 2

struct ShapeClass __Shape;
static void ShapeClassCon(Shape t)
{
ShapeClass vptr=&__Shape;
memcpy(vptr,((Object)t)->__vptr,sizeof(struct ObjectClass));
((ObjectClass)vptr)->__size=sizeof(struct Shape);
((ObjectClass)vptr)->Des = (void (*)(Object))Shape_Des;
((ShapeClass)vptr)->Area = (double (*)(Shape))Object_NoIm;
if (vptr->Scaleable.__offset==0)
vptr->Scaleable.__offset=(char*)&t->Scaleable-(char*)t;
vptr->Scaleable.Scale = (void (*)(Object, double))Object_NoIm;
((ObjectClass)vptr)->__super=((Object)t)->__vptr;
}
Shape Shape_Con(Shape this, char *name) {
{(&this->super)->__vptr=&__Object; };
{
if(((ObjectClass)&__Shape)->__size==0)
ShapeClassCon(this);
((Object)(this))->__vptr=(void*)&__Shape;};
(this)->Scaleable=&__Shape.Scaleable;
if (!StringCon1(&this->name, name))
return ((void *)0);
return this;
}
void Shape_Des(Shape this) {
StringDes(&this->name);
{};
}

struct RectClass __Rect;
static void RectClassCon(Rect t)
{
RectClass vptr=&__Rect;
memcpy(vptr,((Object)t)->__vptr,sizeof(struct ShapeClass));
((ObjectClass)vptr)->__size=sizeof(struct Rect);
((ShapeClass)vptr)->Area = (double (*)(Shape))RectArea;
if (vptr->super.Scaleable.__offset==0)
vptr->super.Scaleable.__offset=(char*)&t->super.Scaleable-(char*)t;
vptr->super.Scaleable.Scale = (void (*)(Object,
double))RectScale;
((ObjectClass)vptr)->__super=((Object)t)->__vptr;
}
Rect RectCon(Rect this, char *name, double w, double h)
{
if (!Shape_Con(&this->super, name))
return ((void *)0);
{
if(((ObjectClass)&__Rect)->__size==0)
RectClassCon(this);
((Object)(this))->__vptr=(void*)&__Rect;
};
(this)->super.Scaleable=&__Rect.super.Scaleable;
this->__h = h;
this->__w = w;
return this;
}
double RectArea(Rect this)
{
return this->__w *this->__h;
}
void RectScale(Rect this, double mag){
this->__w *= mag;
this->__h *= mag;
}

struct CircleClass __Circle;
static void CircleClassCon(Circle t)
{
CircleClass vptr=&__Circle;
memcpy(vptr,((Object)t)->__vptr,sizeof(struct ShapeClass));
((ObjectClass)vptr)->__size=sizeof(struct Circle);
((ShapeClass)vptr)->Area = (double (*)(Shape))CircleArea;
if (vptr->super.Scaleable.__offset==0)
vptr->super.Scaleable.__offset=(char*)&t->super.Scaleable-(char*)t;
vptr->super.Scaleable.Scale = (void (*)(Object,
double))CircleScale;
((ObjectClass)vptr)->__super=((Object)t)->__vptr;
}
Circle CircleCon(Circle this, char *name, double r) {
if (!Shape_Con(&this->super, name))
return ((void *)0);
{
if(((ObjectClass)&__Circle)->__size==0)
CircleClassCon(this);
((Object)(this))->__vptr=(void*)&__Circle;
};
(this)->super.Scaleable=&__Circle.super.Scaleable;
this->__r = r;
return this;
}
double CircleArea(Circle this) {
return 3.14 * this->__r * this->__r;
}
void CircleScale(Circle this, double mag) {
this->__r *= mag;
}
# 10 "test.c" 2
void testShape(Shape s) {
((Object__IsKindOf((Object)(s),(void*)&__Shape)) ? (void)0 : __assert("test.c", 12, "IS_RUNTIME_CLASS(s, Shape)"));
printf("Shape.name="%s", Shape.Area()=%.2f ",
StringToChar(&s->name),
(*((ShapeClass)(((Object)(s))->__vptr))->Area)((Shape)(s)));
}
void testScaleable(Scaleable s) {
((Object__IsKindOf((Object)((Object)((char*)(s)-(*(s))->__offset)),(void*)&__Object)) ? (void)0 : __assert("test.c", 19, "IS_RUNTIME_CLASS(I_TO_OBJ(s), Object)"));
printf("Scaleable.Scale(), ");
(*(*(s))->Scale)((Object)((char*)(s)-(*(s))->__offset) ,2.0 );
}


void main() {
struct Circle circle;
Circle c;
Rect r;
int i;

c = CircleCon(&circle, "Circle", 0.5);
r = (Rect)Object__AllocArr(sizeof(struct Rect), 10);
for (i = 0; i < 10; i++) {
char name[20];
sprintf(name, "Rectangle-%d", i);
RectCon(&r[i], name, (double)i, 0.5);
}

testScaleable(&c->super.Scaleable);
testShape((Shape)c);
for (i = 0; i < 10; i++) {
testScaleable(&r[i].super.Scaleable);
testShape((Shape)&r[i]);
}

(*((ObjectClass)(((Object)(c))->__vptr))->Des)((Object)(c));
Object__DeleteArr((Object)(r));
}