S
Servé Lau
Consider the following code:
struct X { float f; };
struct Y { struct X x; };
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.
Therefore, struct Y can safely be used as a struct X right? 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.
struct X { float f; };
struct Y { struct X x; };
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.
Therefore, struct Y can safely be used as a struct X right? 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.