Skip to content

Instantly share code, notes, and snippets.

@fourdollars
Created December 17, 2009 11:28
Show Gist options
  • Save fourdollars/258690 to your computer and use it in GitHub Desktop.
Save fourdollars/258690 to your computer and use it in GitHub Desktop.
/* Cascaded function calls in C */
#include <stdio.h>
#include <stdlib.h>
typedef struct Object Object;
struct Object {
Object* (*foo)(Object*);
};
static Object* foo(Object* obj)
{
printf("Hello World!\n");
return obj;
}
static Object* createObject(void)
{
Object* obj = (Object*) malloc(sizeof(Object));
obj->foo = foo;
return obj;
}
int main(int argc, char* argv[])
{
Object* obj = createObject();
obj->foo(obj)->foo(obj)->foo(obj)->foo(obj);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment