I wrote this small example piece of OOP being implemented in pure C. I wanted it to be reviewed for the following points:PortabilityPerformanceAnd especiallyUsabilityHow does it lookImplementation: typedef char    XCFString;struct XCFStringStatic {    void *(*initWithCString)(const char *);};struct XCFStringClass {    void (*release)(void);    size_t      length;    char        *value;};// Pointer to current classXCFString **XCFStringCTX    = NULL;// Release methodvoid XCFString__release(void){    assert(XCFStringCTX != NULL);    XCFString *ctx  = *XCFStringCTX;    free(((char *)ctx) - sizeof(struct XCFStringClass));}// Init methodvoid *XCFString__initWithCString(const char *str){    size_t str_len              = strlen(str);    struct XCFStringClass *data = malloc(sizeof(* data) + str_len + 1);    if (!data) return NULL;    char *str_val               = ((char *)data) + sizeof(* data);    memcpy(str_val, str, str_len + 1);    data->length                = str_len;    data->value                 = str_val;    data->release               = XCFString__release;    return data->value;}struct XCFStringStatic XCFStringStatic = {.initWithCString = XCFString__initWithCString};#define priv_getMacroName1(_0, _1, macroName, ...)          macroName#define getMacroName1(macroName, args...)                   priv_getMacroName1(_0, ##args, macroName ## _1, macroName ## _0)(args)#define XCFString_0()       (XCFStringStatic)#define XCFString_1(_var)   (XCFStringCTX=&(_var), ((struct XCFStringClass *)(((char *)_var) - sizeof(struct XCFStringClass))))// Switch between XCFString_0 and XCFString_1, GNUC only#define XCFString(args...)                                  getMacroName1(XCFString, ##args)Test code for main.cint main(int argc, const char **argv){    // Pointer to our String class    XCFString   *string     = NULL;    // Initialize string with Hallo Welt!    string = XCFString().initWithCString(Hallo Welt!);    // Print it as if it were a normal string    fprintf(stderr, String        : %s\n,     string);    fprintf(stderr, Length        : %zu\n,    strlen(string));    // Get the properties from the string    fprintf(stderr, Str.value     : %s\n,     XCFString(string)->value);    fprintf(stderr, Str.length    : %zu\n,    XCFString(string)->length);    // Release the string    XCFString(string)->release();}
Example String Class in C
c;object oriented;strings
I would actually avoid the attempt to mimic C++ syntax. It makes things more complex and exposes unnecessary details.For me it would be equally object-oriented with the following C interface:#ifdef __cplusplus#extern C {#endiftypedef struct XCFString_{    char unused__;} XCFString;XCFString* XCFString_Init();XCFString* XCFString_InitFromCString(char const* sz);size_t XCFString_Length(XCFString const* xcfs);char const* XCFString_Value(XCFString const* xcfs);void XCFString_Release(XCFString* xcfs);#ifdef __cplusplus}#endifThe above interface hides the details while allowing polymorphism; you could e.g. keep the vtable implementation (but hidden) and provide different init-functions.I really don't follow all that macro voodoo but having a mutable shared state - the pointer to current class - seems susceptible to race conditions.