GObject.TypeClass.add_private

### Method

# GObject TypeClassadd_private

deprecated: 2.58 since: 2.4

#### Declaration [src]

    void
    g_type_class_add_private (
      GObjectTypeClass* g_class,
      gsize private_size
    )

#### Description [src]

Registers a private structure for an instantiatable type.

When an object is allocated, the private structures for the type and all of its parent types are allocated sequentially in the same memory block as the public structures, and are zero-filled.

Note that the accumulated size of the private structures of a type and all its parent types cannot exceed 64 KiB.

This function should be called in the type’s `class_init()` function. The private structure can be retrieved using the G_TYPE_INSTANCE_GET_PRIVATE() macro.

The following example shows attaching a private structure MyObjectPrivate to an object MyObject defined in the standard GObject fashion in the type’s `class_init()` function.

Note the use of a structure member “priv” to avoid the overhead of repeatedly calling MY_OBJECT_GET_PRIVATE().

    typedef struct _MyObject        MyObject;
    typedef struct _MyObjectPrivate MyObjectPrivate;

    struct _MyObject {
     GObject parent;

     MyObjectPrivate *priv;
    };

    struct _MyObjectPrivate {
      int some_field;
    };

    static void
    my_object_class_init (MyObjectClass *klass)
    {
      g_type_class_add_private (klass, sizeof (MyObjectPrivate));
    }

    static void
    my_object_init (MyObject *my_object)
    {
      my_object->priv = G_TYPE_INSTANCE_GET_PRIVATE (my_object,
                                                     MY_TYPE_OBJECT,
                                                     MyObjectPrivate);
      // my_object->priv->some_field will be automatically initialised to 0
    }

    static int
    my_object_get_some_field (MyObject *my_object)
    {
      MyObjectPrivate *priv;

      g_return_val_if_fail (MY_IS_OBJECT (my_object), 0);

      priv = my_object->priv;

      return priv->some_field;
    }

Available since: 2.4

Deprecated since: 2.58

Use the G_ADD_PRIVATE() macro with the `G_DEFINE_*` family of macros to add instance private data to a type.

#### Parameters

`private_size`

  Type: `gsize`

  Size of private structure.