C Union Polymorphism within Arrays
arrays, gcc, polymorphism, unions
Solution
The GCC documentation states:
This attribute, attached to a union type definition, indicates that any function parameter having that union type causes calls to that function to be treated in a special way.
In other words, the transparency only applies to function parameters.
Problem
Given the following: ``` typedef struct{ ... ... } A; typedef struct{ ... ... } B; typedef union __attribute__((transparent_union)) { A a; B b; } C; ``` If I declare a function ``` myMethod(C){ ... } ``` The following is legal without explicit casting: ``` A myA; B myB; meMethod(myA); myMethod(myB); ``` (from: "c unions and polymorphism") However, why is the following not allowed: ``` C cArray[2]; c[0]=myA; c[1]=myB; ``` This gives an incompatible type error without explicit casting. Is there any way to avoid explicit casting?