Struct Inheritance in C

Started by
20 comments, last by BeerNutts 10 years, 1 month ago


No need to separate out the typedef and struct definitions in C, and, personally, if you're going to typedef something, it should have the '_t' modifier, not the struct definition that isn't even used.

Technically the _t suffix is reserved by POSIX to declare new types in the future. But then, POSIX also forbids using functions that begin in "to" and "at" so... ph34r.png

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Advertisement

Unless you have dire performance needs use snprintf to manipulate string, it is the best all around function.

On topic, most times I have seen heritage system its was like this:


#include <stdio.h>

#define DEFAULT_BUFFER_SIZE 64

struct Employee_t {
    char name[DEFAULT_BUFFER_SIZE];
    float wage;
};

typedef struct Employee_t Employee;

struct Manager_t {
    Employee super;
    char title[DEFAULT_BUFFER_SIZE];
};

typedef struct Manager_t Manager;

struct Intern_t {
    Employee super;
    char college[DEFAULT_BUFFER_SIZE];
};

typedef struct Intern_t Intern;

...

...

Or even better:


typedef struct {
char name[DEFAULT_BUFFER_SIZE];
float wage;
} Employee_t;

typedef struct {
Employee_t super;
char title[DEFAULT_BUFFER_SIZE];
} Manager_t;

typedef struct {
Employee_t super;
char college[DEFAULT_BUFFER_SIZE];
} Intern_t;
 

No need to separate out the typedef and struct definitions in C, and, personally, if you're going to typedef something, it should have the '_t' modifier, not the struct definition that isn't even used.

Can you point me any advantage other than coding style? I like to split the declarations from the typedef, I find that it has better reabability when the structs are bigger.

What's the point of defining the name of the structure if you're not going to use it? If you have a pointer in the struct point to itself (Link list), then it makes sense. If you're not going to typedef it, then it makes a little sense. But, if you give the struct a name and define a typedef for it, I don't see the point. That's just me.

Also, you've named the structure with *_t...for what purpose? I would've thought the _t defines a type. If you're going to name the struct, using _s would've made more sense in this case.

Personally, I precede my typedef's with a "T", but it just coding style (and, POSIX doesn't have reign over how people choose to define their types).

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This topic is closed to new replies.

Advertisement