Instanced properties vs in class, C orienting objects with glib (GTK +)

1

Without so many detours hopefully I can help with something that has me stranded a whole week, I have a serious confusion with GTK + and its object system, reading the documentation of the GNOME project I understood that uses GType to have a list of all objects and its properties and GObject is an abstract class with all the functions basically required.

Now my confusion is in the two functions _init and class_init , one initializes the instance of the class or the object and the other the class as such, within class_init you have to initialize all the properties, virtual functions etc, but I found the mechanism for the properties of objects, explained here and left very lost. Why the properties do not go in the structure of the object ?, looking for a bit I found this response in this community which half explained me but left me the other half still in doubt, I show you my two structures:

Object structure

struct _WindowPrincipal
{
  GtkWindow *padre;

  GtkMenuBar *menuBar;
  GtkDrawingArea *areaDibujo;
  GtkGrid *grid;        
};

Class structure

struct _WindowPrincipalClass
{
    GtkWindowClass *clasePadre;

    GtkMenuBar (*menu) ();
    void (*design) (WindowPrincipal *self);
};

As you can see they do not have anything of the other world, in _WindowPrincipal I define the parent that is a GtkWindow and the properties of that instance, it has a menu, a drawing area and a grid for the design of the window , in the structure of the class two functions for the design of the menu and the design of the window.

In the function class_init the properties are registered with GParamType , but I do not find logic to this, so then, why do I define the properties in the structure of the object if in class_init I will register them to the class and the object respectively? What logic does that have? Or am I confusing myself with two things that have nothing to do with it? I really can not understand it.

    
asked by Mario 26.02.2018 в 20:16
source

1 answer

2

C does not support inheritance or polymorphism natively because it is not object oriented ... however, there are mechanisms that allow you to simulate this behavior ... but you have to program them by hand.

Thus, a series of utilities must be implemented manually:

  • Type inheritance: In C the concept of inheritance does not exist ... as much as it admits the composition.
  • Polymorphism: Since there are no objects, C will not give you information about whether one type can be safely converted to another (as it does dynamic_cast in C ++)
  • Virtual functions: You have to find a mechanism that allows you to work with virtual functions (a class can override the behavior of the parent class)
  • Encapsulation: In C everything is public. There are no access modifiers so nothing prevents someone from modifying a member variable of your object.

Obviously it is not necessary to implement all these features ... but depending on the design you propose, it may be necessary to implement more than one.

How does GTK work?

C does not support many C ++ features, so if we want to emulate any of these features, we have to program it by hand.

In C ++:

  • The class is a data type and determines the interface of the objects.
  • The object is the variable that is created for a given class

That is:

class POO // <-- clase, es un tipo
{
public:
  int var; // <<-- interfaz de la clase
};

POO poo;      // <<-- poo es un objeto
poo.var = 10; // <<-- accedemos a las propiedades de poo,
              //      que vienen determinadas por su clase

However, in GTK they have chosen to isolate these two concepts for, among other reasons, isolation, reuse:

  • isolation: Function pointers could be modified easily and inadvertently, which would corrupt the application. With this solution the function pointers are out of reach and it is GTK who manages them.
  • reuse: All objects of the same class use the same function pointers, then it is not necessary to replicate those pointers in each created object ... with a single copy there is enough.

So, in GTK, you have a structure to manage the function pointers and another to manage the member variables:

// Objeto: Solo tiene las variables miembro
struct _WindowPrincipal
{
  GtkWindow *padre;

  GtkMenuBar *menuBar;
  GtkDrawingArea *areaDibujo;
  GtkGrid *grid;        
};

// Clase: Gestiona los punteros a función
struct _WindowPrincipalClass
{
    GtkWindowClass *clasePadre;

    GtkMenuBar (*menu) ();
    void (*design) (WindowPrincipal *self);
};

These two classes end up linking together in the GTK core with the subsequent configuration that needs to be done. Note that so far the only contact that these structures have had with GTK is limited to include GTK structures and pointers ... at no time have we told GTK that it should try a new type and even less how it should be treated. / p>

Your initial approach is good but you have not yet configured the GTK environment so you can work with your type.

    
answered by 27.02.2018 / 08:31
source