From: Paul Brannan Date: 2001-07-24T05:09:07+09:00 Subject: [ruby-talk:18375] Re: Ruby as opposed to Python? > In C you can store function pointers inside structures, thus essentially > pass behaviour around. You can not enherit structures, but you can point > from one structure to another to another. Syntactically this is not always > elegant, but conceptually C is very powerful if you want it to be. It comes > down to programming style. Actually, GTK+ has a very interesting way of doing single inheritance using pure C; one struct "derives" from another simply by having an instance of the other structure as its first element: #define GTK_CHECK_CAST(tobj, cast_type, cast) ((cast *) (tobj)) #define GTK_WIDGET(widget) \ (GTK_CHECK_CAST((widget), GTK_TYPE_WIDGET, GtkWidget)) struct GtkObject { ... }; struct GtkWidget { GtkObject object; ... }; struct GtkContainer { GtkWidget widget; ... }; Thus GtkContainer derives from GtkWidget, which derives from GtkObject. So now if we want to cast between a GtkContainer and a GtkWidget, all we have to do is: GtkContainer * container = foo(); GtkWidget * widget = GTK_WIDGET(container); This obviously doesn't work for multiple inheritance, but can be pretty powerful if used correctly (the GTK_CHECK_CAST macro actually does run-time type checking if you compile with the right flags set). I don't know any easy way to do MI in C without using pointers, as you suggested. Paul