Re: Best practise inheritance



On Mon, 20 Mar 2017 16:01:39 +0000
Tristan Van Berkom <tristan vanberkom codethink co uk> wrote:


Use instance private data, this will not need any priv pointer and
can be done with th G_DEFINE_TYPE_WITH_PRIVATE() macro, and another
to lookup your private data inside your C file (under the hood, this
uses negative instance offsets with power nter arithmatic, so public
and private data are on the same allocated memory slice)


So how is instance private data different from the priv field in the
struct? Only because G_DEFINE_TYPE_WITH_PRIVATE was called?
This is (roughly) what I found in the developer docs describing what
the boilerplate parts should look like:
myType.h

#include <gtk/gtk.h>

#define MY_TYPE my_type_get_type ()
G_DECLARE_FINAL_TYPE (MyType, my_type, MY, TYPE, GtkWidget)

/* function declarations */

myType.c

#include "myType.h"

/* Private structure definition. */
typedef struct {
  guint i;
  /* stuff */
} MyTypePrivate;


struct _MyType
{
  GtkWidget parent;

  /* Other members, including private data. */
}

G_DEFINE_TYPE_WITH_PRIVATE (MyType, my_type, GTK_TYPE_WIDGET)

So do I have to add the MyTypePrivat *priv field to the struct _MyType
definition (where the comment is)? And is this (finally) the recommended
way of "inheritance"?


There is no difference for accessing these things as inherited code
or external code: do so with API (no such thing as "protected").


It makes a difference to the compiler I suppose. The nicest way would
be to use
MyType *myWidget;
gtk_widget_set_name (myWidget, "myWidget");
but the pointer types don't match.
Also, gtk_widget_set_name (myWidget->parent, "myWidget"); and
gtk_widget_set_name (GTK_WIDGET (myWidget), "myWidget"); seem a bit
different to me, because the first only affects the internal parent
instance, the second affects the whole custom widget. I do not know
enough about GTK internals if those two are semantically equivalent
anyway.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]