GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Tue Aug 30, 2016 4:53 am

All times are UTC

Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: C inheritance and pointer type casting
PostPosted: Tue Dec 17, 2013 3:14 am 

Joined: Wed Nov 27, 2013 2:11 am
Posts: 2
Hi everyone,

I'm trying to understand GTK's type system and how objects can be derived from others. I'm also trying to understand more thoroughly pointer type casting and exactly what happens when you convert pointers from one type to another (primarily pointers to complex structure types). I found this quick bit of code in gtkbutton.c as an example of what I mean:

static void gtk_button_class_init (GtkButtonClass *klass)
  GObjectClass *gobject_class;
  GtkObjectClass *object_class;
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;

  gobject_class = G_OBJECT_CLASS (klass);
  object_class = (GtkObjectClass *) klass;
  widget_class = (GtkWidgetClass *) klass;
  container_class = (GtkContainerClass *) klass;
  gobject_class->constructor  = gtk_button_constructor;
  gobject_class->dispose      = gtk_button_dispose;
  gobject_class->set_property = gtk_button_set_property;
  gobject_class->get_property = gtk_button_get_property;

  object_class->destroy = gtk_button_destroy;

  widget_class->screen_changed = gtk_button_screen_changed;
  widget_class->realize = gtk_button_realize;
  widget_class->unrealize = gtk_button_unrealize;
  widget_class->map = gtk_button_map;
  widget_class->unmap = gtk_button_unmap;
  widget_class->style_set = gtk_button_style_set;
  widget_class->size_request = gtk_button_size_request;
  widget_class->size_allocate = gtk_button_size_allocate;
  widget_class->expose_event = gtk_button_expose;
  widget_class->button_press_event = gtk_button_button_press;
  widget_class->button_release_event = gtk_button_button_release;
  widget_class->grab_broken_event = gtk_button_grab_broken;
  widget_class->key_release_event = gtk_button_key_release;
  widget_class->enter_notify_event = gtk_button_enter_notify;
  widget_class->leave_notify_event = gtk_button_leave_notify;
  widget_class->state_changed = gtk_button_state_changed;
  widget_class->grab_notify = gtk_button_grab_notify;

  container_class->child_type = gtk_button_child_type;
  container_class->add = gtk_button_add;

  klass->pressed = gtk_real_button_pressed;
  klass->released = gtk_real_button_released;
  klass->clicked = NULL;
  klass->enter = gtk_button_update_state;
  klass->leave = gtk_button_update_state;
  klass->activate = gtk_real_button_activate;
  ... more ...

I notice in the source code for some structures declared throughout GTK that the first member is something like GObject object; and there are comments saying that it has to be the first member in the structure for the "object mechanism to work". What is the significance of that?

As you can see in the code above, they're casting a GtkButtonClass pointer to several other pointer types and then accessing fields of those types. I don't really know how to draw memory diagrams or anything but what is actually happening when those pointers are cast? I mean, if the object coming into the function is a GtkButtonClass that does not have the fields accessed by "widget_class", how can this work?

By the way, GtkButtonClass is declared as follows in gtkbutton.h:
struct _GtkButtonClass
  GtkBinClass        parent_class;
  void (* pressed)  (GtkButton *button);
  void (* released) (GtkButton *button);
  void (* clicked)  (GtkButton *button);
  void (* enter)    (GtkButton *button);
  void (* leave)    (GtkButton *button);
  void (* activate) (GtkButton *button);
  /* Padding for future expansion */
  void (*_gtk_reserved1) (void);
  void (*_gtk_reserved2) (void);
  void (*_gtk_reserved3) (void);
  void (*_gtk_reserved4) (void);

All of this code comes from v2.20.1 of GTK+. Any help is much appreciated.

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC

Who is online

Users browsing this forum: Yahoo [Bot] and 2 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group