Since C doesn't support inheritance, GLib / GTK emulates it by placing base class object as the first member of derived class object. I suppose it is something like this:
struct Parent
{
//...
};
struct Child
{
Parent base;
//...
}
Then we can cast child struct pointer to parent struct pointer:
Child *p_child = make_child();
Parent *p_parent = (Parent*)p_child;
In C++ it can be done using reinterpret_cast:
Parent *p_parent = reinterpret_cast<Parent*>(p_child);
But as far as I know it is UB because Child and Parent are unrelated classes.
Is it undefined behavior in C++? Is it undefined behavior in C?
In GLib / GTK there are converting macros:
GtkWindow *p_window = ...;
GtkWidget *p_widget = GTK_WIDGET(p_window);
I don't know what they actually do. Do they use some special operations to make casting defined behavior?