Polymorphic data structures in C
I am a C beginner with quite a lot of OOP experience (C#) and I am having
trouble understanding how some notion of "polymorphism" can be achieved in
C.
Right now, I am thinking how to capture the logical structure of a file
system using structs. I have a folder that contains both folders and
files. Folders in this folder can contain another files and folders, etc.
My approach:
typedef enum { file, folder } node_type;
struct node;
typedef struct {
node_type type;
char *name;
struct node *next;
struct node *children;
} node;
Is this the best I can do? I have found a lot of posts on "polymorphism in
C", but I would like to see how a polymorphic data structure like this can
be built cleanly and efficiently (in terms of memory wasted on unused
members of those structures).
Thanks.
No comments:
Post a Comment