2

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;

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?

struct parent { int a; }
struct child  { int a; int b; }
  • 1
    en.wikipedia.org/wiki/Type_punning. Seems dodgy to me. – Robert Harvey Dec 27 '16 at 16:03
  • 2
    Implementing inheritance on C (together with a few other things) gave birth to things like C++, Java, C#... really, if you are going for objects, go for an object-oriented language. C is a language on which stuff can go very wrong, very fast unless you really knows how it works under the hood. Object Orientation is not something you can homebrew easily! – T. Sar Dec 27 '16 at 17:14
  • 1
3

For maintenance reasons, go for the first solution of inheriting the parent structure. The C language being very lenient with type-safe programming, it is then safe to pass the new structure as a casted pointer to a base function using the parent. The advantage is that adding a member in the parent will not require changing all descendants. As a side note, this is also what several C++ implementations do, repeating the members automatically instead of adding a level of dereference for each inheritance level.

As an old trick, you could also add preprocessor defines to allow using the base types:

struct parent {
  int parent_i;
  char parent_c;
};

struct descendant {
  struct parent parent_data;
  int descendant_d;
};
#define descendant_i parent_data.parent_i
#define descendant_c parent_data.parent_c

This is what is done in socket structures (check sock_addr). When doing so, be sure to properly namespace your structure members.

0

You are better off repeating the common fields than including one structure in the other, because of the rules for layout of the fields and what scenarios the language is defined to accommodate.

However you might refactor several common fields into a separate struct that is then included by both/all the others, which now follows the above advice.


Note that the example you're providing casts a struct itself, which is a bit unusual. More expected would be casting pointer to struct into pointer to (another) struct.

And as @RobertHarvey says, C is going to be very tolerant of writing type unsafe and potentially buggy code.

  • 3
    In C, the language rules are such that a pointer to a struct can be converted to a pointer to the first element of that struct and back. This guarantee is what GTK (and similar libraries) use to implement inheritance. – Bart van Ingen Schenau Dec 28 '16 at 20:01
  • @BartvanIngenSchenau, ok, good to know. – Erik Eidt Dec 28 '16 at 23:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.