I was working with GTK in one of my projects and I noticed that the library supports inheritance. As you can type-cast a child struct to its parent struct and vice versa. Other than GTK I've never seen this used (so flawlessly):
struct parent p = {5};
struct child c;
c = (struct child)p;
c.b = 1;
//c.a = 5, c.b = 1
Does using the parent struct as the first element do this? As this would seem much more neat. But could padding and aligning interfere?
struct parent { int a; }
struct child { struct parent p; int b; }
Or does rewriting all parent data make it happen?
struct parent { int a; }
struct child { int a; int b; }
EDIT I am NOT looking to implement all pillars of OOP in C. My question is only involving type-casting the data in parent and child structs. I have found that GTK uses GObject to do this, but what must be done to achieve what GObject is doing as far as type casting?