Thread: classes, structs and unions

  1. #1
    Imperator of Darkness Luciferek's Avatar
    Join Date
    Jun 2008
    Location
    Detroit, MI
    Posts
    38

    classes, structs and unions

    Hey
    I was wondering what is the difference between classes unions and structs?
    They all seem to be the same

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Classes and structs are identical except a struct's members are visible by default (public) and a class's is private by default.
    However, many C++ programmers tend to only use structs for POD, plain old data and not as classes, objects.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And a union is different from both struct and class in that ALL of the data members in a union are placed at the same postion [so they are overlaying each other in the same place in memory].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Quote Originally Posted by matsp View Post
    And a union is different from both struct and class in that ALL of the data members in a union are placed at the same postion [so they are overlaying each other in the same place in memory].

    --
    Mats
    Dumb question - How would that work? How would adding to one variable not erase another if they are all overlayed?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by valaris View Post
    Dumb question - How would that work? How would adding to one variable not erase another if they are all overlayed?
    Writing one variable does erase all the others. The whole point of a union is that you can have a, or b, or c, but only one at a time.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by valaris View Post
    Dumb question - How would that work? How would adding to one variable not erase another if they are all overlayed?
    It only works for POD variables -- those without destructors. It doen't do anything to "erase" the variable when written over; it just writes over it. After such a write, only the new variable may be accessed.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    wonder who named it union then. A preacher perhaps, surely not a mathematician.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by indigo0086 View Post
    wonder who named it union then. A preacher perhaps, surely not a mathematician.
    What name would you have chosen?

  9. #9
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    touche.

    I don't know, "rug" perhaps?

  10. #10
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    If you draw a Venn diagram of the variables in your memory, it is a union.

  11. #11
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Ahhh...so of all the data types in the union, the union can only be one of these?

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    A union such as:
    Code:
    union abc
    {
       int i;
       char c;
       float f;
    };
    simply causes i, c & f to begin at the same memory address.

    If you set i to 0x01020304, then read c, you should see 0x01 or 0x04 (depending on whether you're on a Big Endian or Little Endian system).
    Just think of it as having a block of memory and just casting it to int, char, or float...

  13. #13

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    Quote Originally Posted by valaris View Post
    Ahhh...so of all the data types in the union, the union can only be one of these?
    Yes.

    For example,
    Code:
    union  MyUnion  { int n; double d; };
    struct MyStruct { int n; double d; };
    The union allows you to store an integer OR a double (but not both at the same time). The size of the union should be 8 bytes, the first 4 are shared. That means writing to one variable will 'overwrite' the other variable.

    The struct allows you to store an integer AND a double at the same time, because it makes enough room for both variables, which should be 12 bytes.

  14. #14
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Ahh I understand now...
    I don't see them much, what are they used for generally?
    What are they useful for?

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I've seen them used in Windows API. For example the INPUT_RECORD struct looks like this:

    Code:
    typedef struct _INPUT_RECORD { // ir 
    
        WORD EventType; 
        union { 
            KEY_EVENT_RECORD KeyEvent; 
            MOUSE_EVENT_RECORD MouseEvent; 
            WINDOW_BUFFER_SIZE_RECORD WindowBufferSizeEvent; 
            MENU_EVENT_RECORD MenuEvent; 
            FOCUS_EVENT_RECORD FocusEvent; 
        } Event; 
    } INPUT_RECORD;
    This single struct can store data about various events, each requiring different fields. Which event record contains valid data is determined by EventType, other records are just garbage.

    In C++ and high level programming unions are probably very rarely used if ever.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unions & Structs... Puzzled.
    By Sparrowhawk in forum C Programming
    Replies: 10
    Last Post: 12-14-2008, 04:45 PM
  2. unions and structs
    By Ron in forum C Programming
    Replies: 24
    Last Post: 07-24-2008, 10:54 PM
  3. Possible to create methods for data in structs?
    By eccles in forum C Programming
    Replies: 19
    Last Post: 12-15-2004, 09:46 AM
  4. why unions and structs couldnt be initialized ?
    By black in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2002, 05:05 AM
  5. Structs and Unions
    By Encrypted in forum C Programming
    Replies: 4
    Last Post: 11-05-2002, 03:53 AM