C++ Programming Unions
C++ Programming Unions
Log in
Book Discussion
Read Edit
Search
C++ Programming/Unions
< C++ Programming
Main Page Help Browse Cookbook Wikijunior Featured books Recent changes Donations Random book Community Reading room Community portal Bulletin Board Help out! Policies and guidelines Contact us Tools Languages Sister projects Print/export
Contents [hide] 1 union 1.1 Writing to Different Bytes 1.2 Example in Practice: SDL Events 1.3 this
union
Syntax
[edit]
union union-name { public-members-list; private: private-members-list; } object-list; Union is similar to struct (more that class), unions differ in the aspect that the fields of a union share the same position in memory and are by default public rather than private. The size of the union is the size of its largest field (or larger if alignment so requires, for example on a SPARC machine a union contains a double and a char [17] so its size is likely to be 24 because it needs 64-bit alignment). Unions cannot have a destructor. What is the point of this? Unions provide multiple ways of viewing the same memory location, allowing for more efficient use of memory. Most of the uses of unions are covered by object-oriented features of C++, so it is more common in C. However, sometimes it is convenient to avoid the formalities of object-oriented programming when performance is important or when one knows that the item in question will not be extended. union Data { int i; char c; };
converted by Web2PDFConvert.com
// primary event structure in SDL typedef union { Uint8 type; SDL_ActiveEvent active; SDL_KeyboardEvent key; SDL_MouseMotionEvent motion; SDL_MouseButtonEvent button; SDL_JoyAxisEvent jaxis; SDL_JoyBallEvent jball; SDL_JoyHatEvent jhat; SDL_JoyButtonEvent jbutton; SDL_ResizeEvent resize; SDL_ExposeEvent expose; SDL_QuitEvent quit; SDL_UserEvent user; SDL_SysWMEvent syswm; } SDL_Event; Each of the types other than Uint8 (an 8-bit unsigned integer) is a struct with details for that particular event. // SDL_MouseButtonEvent typedef struct{ Uint8 type; Uint8 button; Uint8 state; Uint16 x, y; } SDL_MouseButtonEvent; When the programmer receives an event from SDL, he first checks the type value. This tells him what kind of an event it is. Based on this value, he either ignores the event or gets more information by getting the appropriate part of the union. For example, if the programmer received an event in SDL_Event ev, he could react to mouse clicks with the following code. if (ev.type == SDL_MOUSEBUTTONUP && ev.button.button == SDL_BUTTON_RIGHT) { cout << "You have right-clicked at coordinates (" << ev.button.x << ", " << ev.button.y << ")." << endl; } Note: As each of the SDL_SomethingEvent structs contain a Uint8 type entry, it is safe to access both Uint8 type and the corresponding sub-struct together. While identical functionality can be provided with a struct rather than a union, the union is far more space efficient; the struct would use memory for each of the different event types, whereas the union only uses memory for one. As only one entry has meaning per instance, it is reasonable to use a union in this case. This scheme could also be constructed with polymorphism and inheritance features of object-oriented C++, however the setup would be involved and less efficient than this one. Use of unions loses type safety, however it gains in performance.
this [edit]
The this keyword is a implicitly created pointer that is only accessible within nonstatic member functions of a union (or a struct or class ) and points to the object for which the member function is called. The this pointer is not available in static member functions. This will be restated again on when introducing unions a more in depth analysis is provided in the Section about classes. Category: C++ Programming
This page was last modified on 17 July 2010, at 19:14. Text is available under the Creative Commons Attribution/Share-Alike License; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Privacy policy About Wikibooks Disclaimers Developers Mobile view
converted by Web2PDFConvert.com