Skip to content

Instantly share code, notes, and snippets.

@bertzzie
Created October 18, 2011 08:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bertzzie/1294950 to your computer and use it in GitHub Desktop.
Save bertzzie/1294950 to your computer and use it in GitHub Desktop.
C OO
// Credits to nategoose (http://stackoverflow.com/questions/351733/can-you-write-object-oriented-code-in-c/2733004#2733004)
// modified for clarity
struct stack {
struct stack_type * type;
// privates
};
struct stack_type {
void (* construct)(struct stack * this);
struct stack * (* operator_new)();
void (*push)(struct stack * this, thing * t);
thing * (*pop)(struct stack * this);
int class_member;
}Stack = {
.construct = stack_construct,
.operator_new = stack_operator_new,
.push = stack_push,
.pop = stack_pop
};
stack(struct stack *construct);
// others..
int main(int argc, char** argv)
{
struct stack * st = Stack.operator_new();
if (!st) {
} else {
st->type.push(st, thing2);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment