CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: union

  1. #1
    Join Date
    May 2011
    Posts
    6

    Resolved union

    Why does this report an error
    union U
    {
    CString emplyeeName;
    int id;
    };

    ?
    Yes, I learnt inschool that union does not allow a constructor to exist inside it , but why ?

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: union

    Yes, I learnt inschool that union does not allow a constructor to exist inside it , but why ?
    Because a union isn't a struct. Also, I don't see a constructor. What's the error you get ?

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: union

    Prior to C++0x, unions can only contain POD types with trivial constructors. It's just part of the standard. I believe C++0x relaxes this to some extent, though I am not clear on the details.

    As for why? That should be obvious. A CString, or any object with a nontrival constructor, enforces certain invariants about its own internal memory. A union allows you to trample that memory at will. Clearly, the two concepts don't mix.

    If you need a union-like object which supports all types, consider a boost::variant.

  4. #4
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: union

    My guess is - C2621:
    A union member cannot have a copy constructor.

    Your union CAN have a constructor, but its members - can't.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  5. #5
    Join Date
    May 2011
    Posts
    6

    Re: union

    Oh sorry

    I do something like this

    struct Person
    {
    Person()
    {
    U::emplyeeName="";
    U::id=0;
    }
    union U
    {
    CString empkoyeeName;
    int id;
    };
    };


    it fails. Thanks for replies, need a real answer soon or I will die
    Last edited by OldBarbie; May 17th, 2011 at 01:02 PM.

  6. #6
    Join Date
    Jan 2006
    Location
    Belo Horizonte, Brazil
    Posts
    405

    Re: union

    Quote Originally Posted by Skizmo View Post
    ... Also, I don't see a constructor. What's the error you get ?
    Well, it's not "directly" there, it's in CString. A union member cannot be a class type with a constructor. :-)

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: union

    Do you understand what a union is? Why would you want to use it that way?

  8. #8
    Join Date
    Feb 2002
    Posts
    4,640

    Re: union

    Quote Originally Posted by OldBarbie View Post
    Oh sorry

    I do something like this

    struct Person
    {
    Person()
    {
    U::emplyeeName="";
    U::id=0;
    }
    union U
    {
    CString empkoyeeName;
    int id;
    };
    };


    it fails. Thanks for replies, need a real answer soon or I will die
    The answer is, you can't do that. Why do you want a union? If you really need one like this, then why are you initializing both union members?

    Viggy

  9. #9
    Join Date
    Jan 2009
    Posts
    1,689

    Re: union

    Why would you union a string and an int in the first place? I don't think you understand what a union is. Unions' members aren't really members, they are options of what the piece of memory can be.

    Code:
    union number {
       int i;
       float f;
    };
    
    number n;
    n.i = 1;
    cout << n.i << endl;
    cout << n.f << endl;
    
    n.f = 1.0f;
    cout << n.i << endl;
    cout << n.f << endl;
    I have a feeling the outputs are not going to be what you expect. You will get this

    Code:
    1
    *randomness*
    *randomness*
    1.0
    It's not actually going to be a random value, but it will depend on your architecture. I could tell you what they would be on an intel processor, but mips will be very different. It might also be undefined if the members aren't the same size, I forget the rules to how unions get padded and offset.

  10. #10
    Join Date
    Feb 2002
    Posts
    4,640

    Re: union

    A union's size is the size of the biggest member. IIRC, floats are 4 bytes, so 'number' would have a sizeof 4 (since int's are also 4 bytes). If the float were a double, then your union would be 8 bytes in size.

    Viggy

  11. #11
    Join Date
    Jan 2009
    Posts
    1,689

    Re: union

    I didn't say that the example I posted was that case, I was just saying that it's a more advanced case, that might be undefined.

    What would happen here?

    Code:
    union foo {
       bool b;
       short s;
       int i;
    };
    Is it defined where the bits for each are stored, or is that up to the compiler?

  12. #12
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: union

    Quote Originally Posted by OldBarbie View Post
    Oh sorry

    I do something like this

    struct Person
    {
    Person()
    {
    U::emplyeeName="";
    U::id=0;
    }
    union U
    {
    CString empkoyeeName;
    int id;
    };
    };


    it fails. Thanks for replies, need a real answer soon or I will die
    Hopefully you are still alive and realized that you should just eliminate the union and have two class attributes employeeName and id. You want the person to have both of those attributes, not one or the other. Honestly, it has been a long time since I have seen a good use for a union.

  13. #13
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: union

    Quote Originally Posted by kempofighter View Post
    Honestly, it has been a long time since I have seen a good use for a union.
    I'm pretty sure that since I started programming in C++ I've never used one.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  14. #14
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: union

    Quote Originally Posted by JohnW@Wessex View Post
    I'm pretty sure that since I started programming in C++ I've never used one.
    Not even a VARIANT?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  15. #15
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: union

    Quote Originally Posted by VladimirF View Post
    Not even a VARIANT?
    No.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured