Instead of this:
Code:
struct my_class
{
void (*func1)( struct my_class *, ... );
void (*func2)( struct my_class *, ... );
void (*func3)( struct my_class *, ... );
};
my_class *foo = new_my_class( ... );
foo->func1( foo, ... );
Do this:
Code:
struct my_class;
struct my_class_vtable
{
void (*func1)( struct my_class *, ... );
void (*func2)( struct my_class *, ... );
void (*func3)( struct my_class *, ... );
};
struct my_class
{
struct my_class_vtable *vtable;
};
struct my_class *foo = new_my_class( ... );
foo->vtable->func1( foo, ... );
There is absolutely no point in wasting tons of memory keeping the function pointers in every instance of the "class."