Possible Duplicate:
Can you write object oriented code in C?
Hi!
Just for the fun of it, I've been experimenting these last two days with creating a very simple, very straightforward object environment in pure C. I've been toying around with macros, dynamic linking, type-description structures and the like, and I've arrived at the following:
string_o str = new(String, "hello world");
list_o list = new(List);
List.pushf(list, str);
printf("In the list: \"%s\"\n",
String.text(List.popf(list)));
delete(list);
delete(str);
Looks and works kinda nice, but I can't figure a way to fake instance methods. I can't get past Class.function(instance)
, not without global macro replacements for function names, which defeats the purpose of encapsulation.
Again, this is an experiment, just for the challenge and the fun =). Can you guys help me figure out a way to do this? I don't want to use additional preprocessing, only plain C and GCC macros.
edit> forgot to say -- I don't want each instance to contain the function pointers in its structure. That would give me method syntax alright, but it would mean that a 4-byte data object would have a dozen function pointers copied over to each instance. That's kinda like cheating =P haha
Thanks in advance!