For a typedef
of a struct
in C, I can't do this:
typedef struct {
unsigned id;
node_t *left;
node_t *right;
} node_t;
because node_t
is not known until it is defined, so it can't be used in its own definition. A bit of a Catch-22. However, I can use this workaround to make the desired self-referential type:
typedef struct node_s node_t;
struct node_s {
unsigned id;
node_t *left;
node_t *right;
};
Similarly, I would like to do something like this for a C++ container referring to itself:
typedef pair<unsigned, pair<node_t *, node_t * > > node_t;
but of course, the compiler complains that it's never heard of node_t
before it's defined node_t
, as it would for the struct typedef
above.
So is there a workaround like for the struct
? Or some better way to do this? (And no, I don't want to use void
pointers.)
Self Referential structures are those structures that have one or more pointers which point to the same type of structure, as their member. In other words, structures pointing to the same type of structures are self-referential in nature. Example: CPP.
It should be remembered that a pointer to a structure is similar to a pointer to any other variable. A self referential data structure is essentially a structure definition which includes at least one member that is a pointer to the structure of its own kind.
A self-referential structure is one of the data structures which refer to the pointer to (points) to another structure of the same type. For example, a linked list is supposed to be a self-referential data structure. The next node of a node is being pointed, which is of the same struct type.
You can self-reference a struct pointer if you name it (i.e. typedef struct <name> {...}
). I usually use the following idiom:
typedef struct _node_t { // "Temporary" name "_node_t"
unsigned id;
struct _node_t *left; // Have to use "struct _node_t"
struct _node_t *right;
} node_t; // "Final" name
That basically applies the previous answers to actual code.
You can do it like this:
struct node_t : std::pair<unsigned, std::pair<node_t *, node_t * > >
{};
After struct node_t
the compiler knows that the type with name node_t
exists, similar to a forward declaration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With