In gobject, we usually place the function to be inherited from the subclass in the class structure for the inherited class to be reloaded. The general logic is that after the inherited class is overloaded, the custom logic is implemented, and then the default Implementation of the function of the parent class is called. This will cause a problem: multiple child classes may call a function default Implementation of the parent class at the same time. In this way, the implementation of these functions of the parent class must be thread safe.
The reason for not placing these function pointers in the instance structure of the parent class is that, when the child class is overloaded, the default Implementation of the parent class cannot be called, this will cause code redundancy and difficulty in maintenance.
To verify the above idea, I wrote a program. Parent class Dad, two inherited classes son and daughter. Through the test program, we can see that in a dad_print function opened by the parent class, both son and daughter overload this function. When both son and daughter call the default Implementation of the parent class dad_print, the addresses of the two function pointers are the same. Therefore, according to this test program and the gobject documentation, the memory structure and initialization logic of gobject should be as follows:
1. g_object_new creates a gobject. 2. For the first g_object_new, from the parent class, all the way down until the final subclass, the class structure of the class will be created. In its class structure, because the first member is the class structure of the parent class, in the memory, the class structure of the inherited class should be the class structure of the parent class, next we will inherit the class's own. 3. Each time g_object_new, the instance structure will be new. It is also the parent class's instance structure starting down, and then the subclass's instance structure. Because there may be multiple instances of a class, there is no such thing as parent_instance, only parent_class.
The above description should be shown as follows:
Test code:/files/super119/gobjclsfuncreent.zip