Live data from Hacker News

Instance Model Programming in C

slkpg.byethost7.com

1–10 of 22 posts

Re: Instance Model Programming in C

#3
isn't this just the normal approach used in complex c code, as described in, for example, "c interfaces and implementations" by hanson? a struct with function pointers and state (plus, optionally, macros to sugar away the repetition).

i don't understand what's new here. anyone?

Re: Instance Model Programming in C

#4
Advising anyone trying to make object oriented programing in C to read Object-Oriented Programing With ANSI-C [0][1]. The book more clearly explains how to make classes, inheritance, dynamic type checking. Don't follow the instruction to the letter, but try to understand and write your own version. ( Not for beginners in C though )

[0]: http://www.cs.rit.edu/~ats/books/ooc.pdf

[1]: http://www.cs.rit.edu/~ats/books/ooc-02.01.04.tar.gz ( source code)

Re: Instance Model Programming in C

#5
> Except in a database, the data in most programs is a minor player.

I guess that depends on what your definition of "most" is. A large portion of C code either depends on hardware interfaces or is intended to be optimized. In both cases, the layout of your memory (data) is extremely important to an implementation.

One of the biggest criticisms of OOP is that it tends to obscure data layout, which isn't always an implementation detail. Interestingly, the patterns described here are subject to the same criticisms.

Re: Instance Model Programming in C

#6

isn't this just the normal approach used in complex c code, as described in, for example, "c interfaces and implementations" by hanson? a struct with function pointers and state (plus, optionally, macros to sugar away the repetition). i don't understand what's new here. anyone?

It's similar to most of the complex C code I have been exposed to except for one thing: namespace prefixes. Almost all reusable C components have to have a name prefix or risk name collisions with other code.

Re: Instance Model Programming in C

#7
The simplest way to do OOP in C is to create modules with opaque handles.

Below shows how i use string and string list "classes" in C.String list "class" inherits string "class" seamlessly.

https://github.com/mhogomchungu/zuluCrypt/tree/master/zuluCr...

respective easier to read header files are below:

https://raw.github.com/mhogomchungu/zuluCrypt/master/zuluCry...

https://raw.github.com/mhogomchungu/zuluCrypt/master/zuluCry...

Re: Instance Model Programming in C

#8
Much of C++ made more sense to me when I read Inside the C++ Object Model by Stanley Lippman[1]. It helped me internalize the fact that C++ was originally implemented as a preprocessor transformation into C (Stroustrup's Cfront). Thus many C++ mechanisms can be understood as elaborate syntactic sugar.

[1] http://www.amazon.com/Inside-Object-Model-Stanley-Lippman/dp...

Re: Instance Model Programming in C

#10
This is pretty awesome. GNOME itself is all written in pure C using glib, which also contains GObject, a beautiful OOP approach for the C programming language. It has a dynamic type system, supports meta programming...

Very few people know it, but GNOME's GTK interface is fully object oriented, and BTW when you click a GTK button you are clicking on an instance of the class GtkButton that inherits from GtkBin which inherits from GtkWidget. it's a beautiful thing!

Also because GObject is based on a lot of conventions it makes really easy to generate bindings to other programming languages automatically, so basically you can write you library in C with OOP approach using GObject, and for free you get your library easily bound to python, ruby, js or any other language.

https://developer.gnome.org/gobject/stable/

Post reply on HN