J
jacob navia
As you may have noticed, when I proposed the iterator object I splitted
the object into a public part (the first 3 fields) an an undisclosed
part specific to each iterator.
This inheritance implementation looks like this:
In the header file:
typedef struct PublicStruct {
// public fields
} Public;
In the implementation file (.c) we do:
typedef struct Inherited {
Public p;
// follow the private fields
} Private;
The interface for all functions is just the public structure:
void fn1(Public *);
void fn2(Public *);
The implementation of fn1 however, does this:
void fn1(Public *p) {
Private *pri = (Private *)p;
// Work with the private struct.
// It is assumed that the pointer
// handed down is actually a private
// pointer
}
Obviously this doesn't have any compiler support
but works quite well in practice. You can add a
"magic" number somewhere in the private struct
in the debug version to catch mistakes when passing
the structures.
Obviously you aren't supposed to copy the public object since you do not
know what is behind. A way to avoid that would be to add a VLA:
typedef struct PublicStruct {
// public fields
// ...
// private fields follow:
char private[];
} Public;
YES I KNOW.
C++/Java/C# do this MUCH better, but they have other drawbacks
that make a pure C solution MUCH better. Thank you.
the object into a public part (the first 3 fields) an an undisclosed
part specific to each iterator.
This inheritance implementation looks like this:
In the header file:
typedef struct PublicStruct {
// public fields
} Public;
In the implementation file (.c) we do:
typedef struct Inherited {
Public p;
// follow the private fields
} Private;
The interface for all functions is just the public structure:
void fn1(Public *);
void fn2(Public *);
The implementation of fn1 however, does this:
void fn1(Public *p) {
Private *pri = (Private *)p;
// Work with the private struct.
// It is assumed that the pointer
// handed down is actually a private
// pointer
}
Obviously this doesn't have any compiler support
but works quite well in practice. You can add a
"magic" number somewhere in the private struct
in the debug version to catch mistakes when passing
the structures.
Obviously you aren't supposed to copy the public object since you do not
know what is behind. A way to avoid that would be to add a VLA:
typedef struct PublicStruct {
// public fields
// ...
// private fields follow:
char private[];
} Public;
YES I KNOW.
C++/Java/C# do this MUCH better, but they have other drawbacks
that make a pure C solution MUCH better. Thank you.