c++11 - C++ - Is there a way to create alias for field -
is there way create alias class/stuct filed in c++11 ? mean
i've got class
class vector4 { public: float x,y,z,w; }
i got alias
typedef vector4 color;
is there way create aliases vector4 x,y,z,w fields work color r,g,b,a ?
just define vector4 this, using anonymous unions (without anonymous structs, though common extension).
typedef struct vector4 { union{float x; float r;}; union{float y; float g;}; union{float z; float b;}; union{float w; float a;}; } vector4; typedef vector4 color;
if cannot redefine vector4, might instead define layout-compatible standard-layout class , use dreaded reinterpret_cast
.
works because standard layout classes having layout-compatible members compatible.
struct color { float r, g, b, a; }
standard quote:
a standard-layout class class that:
— has no non-static data members of type non-standard-layout class (or array of such types) or reference,
— has no virtual functions (10.3) , no virtual base classes (10.1),
— has same access control (clause 11) non-static data members,
— has no non-standard-layout base classes,
— either has no non-static data members in derived class , @ 1 base class non-static data members, or has no base classes non-static data members, and
— has no base classes of same type first non-static data member.a standard-layout struct standard-layout class defined class-key struct or class-key class.
standard-layout union standard-layout class defined class-key union.
Comments
Post a Comment