I write a string structure as follows.
typedef struct string string;
struct string
{
int length;
char* content;
void (*init)(string*, const char*);
void (*print)(string*);
};
void print(string* a)
{
printf("%s", a->content);
}
void init(string* a, const char* b)
{
a->init = init;
a->print = print;
a->length = strlen(b);
a->content = (char*)malloc(sizeof(char) * strlen(b));
strcpy(a->content, b);
}
int main()
{
string a;
init(&a, "Hello world!\n");
a.print(&a);
}
I tried to mimic ooc here but I can't figure out a better way. For example, is there a possible way to make print(&a) like: a.print, without passing a pointer to itself to the function, like an implicit *this pointer does in other language?