Best practise inheritance



First of all, inheritance may be the wrong word here in plain c, but I
don't know how else to name it.

In different projects I see different approaches how to derive custom
widgets from existing ones. I can roughly group them into 2 to 3.

1) The header only has a typedef to make the struct opaque. All
variables needed are put into the struct in the .c file.

myType.h
typedef struct _MyType  MyType;

myType.c
struct _MyType
{
  GtkWidget     *parent;
  /* additions */
  guint          i;
  ...
};

2a) The header defines a private struct, and all variables needed are
put into this private struct.

myType.h
typedef struct _MyTypePriv MyTypePriv;
typedef struct _MyType     MyType;

myType.c
struct _MyTypePriv
{
  GtkWidget     *parent;
  /* additions */
  guint          i;
};

struct _MyType
{
  MyTypePriv    *priv;
};

2b) Similar to 2a, but the parent is put in the "main" struct, not the
private part.

myType.h
typedef struct _MyTypePriv MyTypePriv;
typedef struct _MyType     MyType;

myType.c
struct _MyTypePriv
{
  /* additions */
  guint          i;
};

struct _MyType
{
  GtkWidget     *parent
  MyTypePriv    *priv;
};

So my first question: What is the best way here? And are there
(functional) differences between these?

And my second question is closely related: How to access "inherited"
properties, or call "inherited" functions? I see these variants in the
following examples, where I have:
MyType *myWidget;

1) gtk_widget_set_name (GTK_WIDGET (myWidget), "myWidget");
2) gtk_widget_set_name (myWidget->parent, "myWidget");
3) gtk_widget_set_name (myWidget->priv->parent, "myWidget");

I am looking forward to helpful replies.
Thanks and kind regards


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