title: GObject and inheritance
body: <p>I was doing some serious software on Glib. And I realized that there's some topics I don't really understand. IRC also didn't help so...</p>
  
  <p>When we are doing inheritance, we can have two clases. The first one A inherits from GObject directly, B inherits from A. Then I came to this:</p>
  
  <p><a href="https://developer.gnome.org/gobject/stable/chapter-gobject.html" rel="nofollow noreferrer">https://developer.gnome.org/gobject/stable/chapter-gobject.html</a></p>
  
  <pre><code>static void
  viewer_file_constructed (GObject *obj)
  {
    /* update the object state depending on constructor properties */
  
    /* Always chain up to the parent constructed function to complete object
     * initialisation. */
    G_OBJECT_CLASS (viewer_file_parent_class)-&gt;constructed (obj);
  }
  
  static void
  viewer_file_class_init (ViewerFileClass *klass)
  {
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
  
    object_class-&gt;constructed = viewer_file_constructed;
  }
  </code></pre>
  
  <p>But when you have this kind of arrangement. The child class does this: object_class->constructed = viewer_file_constructed; in B overriding in fact the only memory address for constructed. So it means that G_OBJECT_CLASS (viewer_file_parent_class)->constructed (obj); will call B->constructed recursively. And it's not what we want. </p>
  
  <p>Maybe I don't understand but I suppose that the memory structure in B is as this:</p>
  
  <pre><code>struct _B
  {
    A parent_instance;
  
    /* instance members */
  };
  </code></pre>
  
  <p>The internal representation should be something like:</p>
  
  <pre><code>[   Gobject struct memory ]
  [                         ]
  [   Gobject variables     ]
  [   A struct memory       ]
  [   A variables           ] 
  [                         ]
  [   B struct memory       ]
  [   B variables           ] 
  </code></pre>
  
  <p>So the GObject memory when casting in B is the shared by B and A classing. And all addresses are the same on the same i...</p>
  
  <ul>
  <li>G_OBJECT_CLASS (B)->constructed</li>
  <li>G_OBJECT_CLASS (A)->constructed</li>
  </ul>
  
  <p>Is this correct?
  So if I want to overwrite constructed... Do I have to save the pointer that had before and then overwrite it in init with mine? So I can call the original one after I do my processing?</p>
  
  <p>The same applies with the properties. Because A defines it's properties by an enum, that can go from 0 to N.</p>
  
  <p>So I suppose that B properties should start at N, not 0. Otherwise the properties of A are handled by B and maybe with different data structures, and names. </p>
  
  <p>Check this:
  <a href="https://developer.gnome.org/gobject/stable/gobject-properties.html" rel="nofollow noreferrer">https://developer.gnome.org/gobject/stable/gobject-properties.html</a></p>
  
  <pre><code>enum
  {
    PROP_FILENAME = 1,
    PROP_ZOOM_LEVEL,
    N_PROPERTIES
  };
  </code></pre>
  
  <p>If both clases defines a property with index 1. There will be a problem there because glib doesn't know who should handle. I suppose the child class B will handle it but improperly because maybe B is waiting, say a PROP_DIRECTORY, but since the index is the same. Would glib able to send to right instance?</p>
  
  <p>I can only thing it will work if on register glib will add some offset depending on hierarchy level. Can someone explain how this really works? I cannot find any document with desired technical detail.</p>