Is strongly-typed programming conceivable for C code using gcc ?

John Love-Jensen eljay@adobe.com
Wed Sep 19 15:53:00 GMT 2007


Hi Alexandre,

Taking Dima's solution in conjunction with the way Apple solved their
similar problem by using opaque types in Cocoa and Carbon:

struct CW_C_MsgUp_s;
typedef struct CW_C_MsgUp_t* CW_C_MsgUp_t;

struct CW_C_MsgDown_s;
typedef struct CW_C_MsgDown_t* CW_C_MsgDown_t;

This will insulate your implementation of those opaque types from the public
header files (note that the structs are forward references, and not declared
publically), and allow you some measure of type safety in C.

Apple uses a factory function to create their opaque types, and for most
opaque types the CFRelease function to destruct them (using a reference
counting mechanism).  That's the role that CW_C_MsgUp_new() and
CW_C_MsgDown_new() provides.

As far as them being pointers... I believe that you will want them to be
pointers baked into the typedef.

The one change to your code would be to change from this...

CW_C_MsgUp_t *my_up_msg;
CW_C_MsgDown_t *my_down_msg;

...to this...

CW_C_MsgUp_t my_up_msg;
CW_C_MsgDown_t my_down_msg;

HTH,
--Eljay



More information about the Gcc-help mailing list