From: Jason M. <ko...@gm...> - 2008-08-23 21:03:56
|
Henri Häkkinen wrote: > Somebody mentioned in the OpenGL forums that we should avoid the use > of anonymous structures. Is this worth the concern? > > For example, compare these: > > typedef struct GLMmat2f_s { > union { > GLfloat m[4]; > struct GLMmat2fdata_s { > GLfloat m00, m01, m10, m11; > }; > }; > } GLMmat2f; > > vs. > > typedef struct { > union { > GLfloat m[4]; > struct { > GLfloat m00, m01, m10, m11; > }; > }; > } GLMmat2f; > > We need to enclose the mXX variables into a struct in order for the > union to take them as a whole. > > -- > Henri 'henux' Häkkinen I would suggest that those struct definitions violate the design principle of /simplicity/. You need to pick one: either a matrix is an array of floats or it is a struct with member fields. It should not be both. If you're having to use a /union/, you've already done something wrong. Unions are confusing and scary to neophyte programmers. I'd prefer that a matrix were defined as: typedef GLfloat[4] GLMmat2f; It's simpler and gets the job done without being confusing to a programmer as to what is going on. Yes, it will need to have conventions associated with it, but that's a small price to pay for the obviousness of what the matrix is doing. |