The GObject library ( forms the basis of the plain C but object-oriented paradigm used in GLib, GTK+ and other GNOME libraries. It is somewhat awkward to get used to, but definitely has it's pros, namely its high portability and its ability to easily bind it to other languages (scripting languages like python, or "proper" languages like C++).

The GObject paradigm supports inheritance, interfaces, runtime type identification, polymorphism, safe casting, and probably more OO features i dont know :).

The basic idea when you're developing a new class is that your main object struct features a copy of the parent as its first member, like so:
typedef struct _MyObject MyObject;
struct _MyObject {
  GObject __parent__;
  gint foo;
this defines the basic structure used for objects of class "MyClass", deriving directly from GObject. You can safely treat a MyObject* as a GObject* (usually using G_OBJECT(obj) to safecast). You then define the class structure for your class, basically manually setting up a virtual function table:
typedef struct _MyObjectClass MyObjectClass;
struct _MyObjectClass {
  GObjectClass __parent__;
  gint (* get_foo) (MyObject * self);
You will probably also want some other (non-virtual) functions:
void my_object_do_something( MyObject *self );

Then, after some more definitions and #defines for casting, etc, in your corresponding .c file you will do things like
my_object_get_type (void)
  static GType type = 0;
  if (type==0) {
    static const GTypeInfo info = {
      sizeof (MyObjectClass),
      (GBaseInitFunc) NULL,
      (GBaseFinalizeFunc) NULL,
      (GClassInitFunc) my_object_class_init,
      (GClassFinalizeFunc) NULL,
      sizeof (MyObject),
      (GInstanceInitFunc) my_object_init,
  type = g_type_register_static (G_TYPE_OBJECT, "MyObject", &info, (GTypeFlags)0);
In my_object_class_init, you will setup the manual vtable, classwide data members, and GObject properties and signals.

I've left out a lot of detail here, to give you just an overview of the philosophy of GObject. Have a look at any GObject-based library, and if you hate that syntax just as i do, you might want to have a look at the gob2 preprocessor, which gives you the possibility to derive new classes in a java-like syntax. You will still have a lot of macros in your code, though, and will have to understand the underlying functionality.

Log in or register to write something here or to contact authors.