In article <bs**********@news3.tilbu1.nb.home.nl>,
Servé Lau <i@bleat.nospam.com> wrote:
[one "struct" contains another whole, so that &y.x has type "struct X *",
and:]
void func(struct X *x) {}
int main(void) {
struct Y y;
func(&y);
return 0;
}
Ok, there's two structs. One struct X with some member and struct Y that has
a struct X as the first member. In OO terms we say that Y inherits from X.
In "proper" OO terms the member named y.x (of type "struct X")
should be able to go anywhere, not just first.
Therefore, struct Y can safely be used as a struct X right?
In C89 and C99, only via conversions.
Is the above legal code, can we call func() without casting Y * to X *?
My compiler accepts it without warning and I think it should.
C89 and C99 both require diagnostics.
Plan 9 C, which is a different language from both C89 and C99,
allows this kind of call. It works even if "y.x" is not the
first member, too -- the call has the same effect as func(&y.x).
However, the definition for struct Y must read rather differently:
struct Y {
int any, stuff, you, like;
struct X; /* inherit all of struct X's members */
int more, things, ifdesired;
};
I am not quite sure what Plan 9 C says must occur if "struct X"
has members whose names conflict with those of "struct Y" (exclusive
of "struct X" of course). Moreover, what does it mean if you
try to inherit from the same datatype more than once? For
instance:
struct Point { int x, y; };
struct Rectangle {
Point; /* e.g., upper left corner */
int w, h; /* width and height */
};
is OK, but what about
:
struct Rectangle {
Point;
double x; /* error? ok? */
};
and clearly:
struct Rectangle {
Point; /* upper left */
Point; /* lower right */
};
is right out. :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it
http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.