Skip to content

Instantly share code, notes, and snippets.

@coastwise
Created June 17, 2011 16:55
Show Gist options
  • Save coastwise/1031799 to your computer and use it in GitHub Desktop.
Save coastwise/1031799 to your computer and use it in GitHub Desktop.
one way to implement vtables in C
/* Original Author: Tom Gunn
* http://www.daniweb.com/software-development/c/threads/228277
*/
#include <stdio.h>
/* class definitions */
typedef struct Base
{
void (**vtable)();
int _x;
} Base;
typedef struct Child
{
void (**vtable)();
/* begin base class slice */
int _x;
/* end base class slice */
int _y;
} Child;
/* class method implementations */
void Base_ToString(Base const* obj) { printf("Base: (%d)\n", obj->_x); }
void Child_ToString(Child const* obj) { printf("Base: (%d,%d)\n", obj->_x, obj->_y); }
/* vtable implementation */
enum { Call_ToString };
void (*Base_Vtable[])() = { &Base_ToString };
void (*Child_Vtable[])() = { &Child_ToString };
/* virtual method implementation */
void ToString(Base const* obj)
{
obj->vtable[Call_ToString](obj);
}
int main()
{
/* pick the vtable for objects at compile time */
Base base = {Base_Vtable, 123};
Child child = {Child_Vtable, 456, 789};
Base* a = &base;
Base* b = (Base*)&child;
/* call the virtual methods */
ToString(a);
ToString(b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment