Results 1 to 5 of 5

Thread: C++ question?

  1. #1
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default C++ question?

    Hi guys,
    Till now I have never implemented a union in my code and have rarely seen union used anywhere...Also I was just wondering under what situations we should use a structure and when we should use a union?

    Any help will be highly appreciated.
    Thankx

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: C++ question?

    To be honest I have never seen a union in C++ (I have seen many in C).
    A structure is used when you need to store more than one piece of data (like a record). A union is used when you need to store a piece of data which can be of different types (but you can only access one of the pieces), for example:

    Qt Code:
    1. union {
    2. int intvalue;
    3. float floatvalue;
    4. }
    To copy to clipboard, switch view to plain text mode 
    Then you can assign data to either intvalue or floatvalue, but not to both at once (the data will be overwritten if you do).

    I think in C++ unions are rarely used because you can achieve the same in a much cleaner way - for example using inheritance.

  3. #3
    Join Date
    Sep 2006
    Posts
    27
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: C++ question?

    A structure is a class with only public members. Shouldn't be too hard to figure out when to use that..
    Unions on the other hand are aggregate quantities like structs, except that each element of the union has offset 0, and the total size of the union is only as large as is required to hold its largest member. Only one member of a union may be "active" at a time. (thank you, google)
    Unions are indeed rarely used, but they do have very interesting applications, eg: http://www.informit.com/guides/conte...eqNum=271&rl=1

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: C++ question?

    Quote Originally Posted by stinos View Post
    A structure is a class with only public members. Shouldn't be too hard to figure out when to use that..
    You can have private members in a struct as well. The difference is that struct members are public by default and class members are private by default.

    Quote Originally Posted by stinos View Post
    Unions are indeed rarely used
    They use unions in Qt, for example in QVariant and some container classes as well..
    J-P Nurmi

  5. #5
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: C++ question?

    Quote Originally Posted by vermarajeev View Post
    Also I was just wondering under what situations we should use a structure and when we should use a union?
    A union is a set of "memory sharing variables". In example

    Qt Code:
    1. union myUnion
    2. {
    3. int iValue;
    4. float fValue;
    5. };
    6.  
    7. struct myStruct
    8. {
    9. int iValue;
    10. float fValue;
    11. };
    12.  
    13. myStruct myS;
    14. myS.iValue = 1;
    15. myS.fValue = 2.0;
    16.  
    17. myUnion myU;
    18. myU.iValue = 1;
    19. myU.fValue = 2.0;
    20.  
    21. cout << myS.iValue << " " << myS.fValue << endl;
    22. cout << myU.iValue << " " << myU.fValue << endl;
    To copy to clipboard, switch view to plain text mode 

    print something like

    Qt Code:
    1. 1 2.0
    2. <some_value> 2.0
    To copy to clipboard, switch view to plain text mode 

    this because when you assign value to myU.fValue you set memory area use by myU.iValue

    Union are useful to share the same memory area amoung several variables.
    In Example this function

    Qt Code:
    1. void printSwappedByte(unsigned short _val)
    2. {
    3. union
    4. {
    5. unsigned short sV;
    6. unsigned char cV[2];
    7. };
    8.  
    9. sV = _val;
    10. cout << (unsigned int) cV[1] << " " << (unsigned int) cV[0] << endl;
    11. }
    To copy to clipboard, switch view to plain text mode 

    print val bytes in revers order;
    A camel can go 14 days without drink,
    I can't!!!

Similar Threads

  1. QThread exit()/quit() question
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 28th August 2006, 14:38
  2. simple thread layout question
    By mhoover in forum Qt Programming
    Replies: 1
    Last Post: 12th August 2006, 11:02
  3. Beginner C++ question
    By masoroso in forum General Programming
    Replies: 2
    Last Post: 19th April 2006, 14:15
  4. Replies: 6
    Last Post: 19th March 2006, 18:41
  5. simple question on Class-Members
    By mickey in forum General Programming
    Replies: 7
    Last Post: 4th February 2006, 22:37

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.