"Tomás Ó hÉilidhe" <to*@lavabit.comwrote in message
news:Xn**************************@194.125.133.14.. .
:
>I was wondering if there are any open source compilers out there to
translate the C++/Java/C#-like object model to C. Thanks,
David
Hidden pointers is what OO is all about for the most part.
The following in C++:
[...]
>
is implemented as:
typedef struct VTable_Circle {
double (*GetArea)(Circle*);
} VTable_Circle;
typedef struct Circle {
VTable_Circle const *vtable;
double radius;
} Circle;
double GetArea(Circle *const this)
{
return 3.14 * this->radius * this->radius;
}
VTable_Circle vtable_circle = { GetArea };
void ConstructCircle(Circle *const p)
{
p->vtable = vtable_circle;
}
FWIW, you can use some macros to implement the interface so you don't have
to write code like:
Circle c;
ConstructCircle(&c);
c.vtable->GetArea(&c);
Something like:
#define Circle_GetArea(mp_this) ( \
(mp_this)->vtable->GetArea((mp_this)) \
)
Now you can do:
Circle c;
ConstructCircle(&c);
Circle_GetArea(&c);
Also, if you pick some standard function names in the vtable, you could do
some abstract interface stuff... Something like:
typedef struct VTable_Circle {
void (*Object_Destroy) (Circle*);
double (*GetArea)(Circle*);
} VTable_Circle;
#define Object_Destroy(mp_this) ( \
(mp_this)->vtable->Object_Destroy((mp_this)) \
)
and write:
Circle c;
ConstructCircle(&c);
Circle_GetArea(&c);
Object_Destroy(&c);
The Object_Destroy function will work for any object which follows the
"standard" vtable naming convention.
Here is a more complete example:
http://groups.google.com/group/comp....106926ba5db19f
IMHO, it makes things "cleaner"... And in some respects, I like it better
than C++...
;^)