CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: union issues.

  1. #1
    Join Date
    Jan 2007
    Posts
    143

    Wink union issues.

    #include <iostream.h>

    class vehicle
    {
    public : vehicle()
    {
    }
    int i;
    float e;
    };

    main()
    {
    union a
    {
    char k;
    int j;
    vehicle obj;
    };
    cout<<sizeof(a)<<endl;
    return 0;
    }
    this code gives compilation error as shown below error C2620: union 'a' : member 'obj' has user-defined constructor or non-trivial default constructor.

    But without constructor same code prints 8 bytes...Need to know why defining constructor gives compilation problem ..pls clarify..

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: union issues.

    Are you banned by MSDN?
    Compiler Error C2620
    Victor Nijegorodov

  3. #3
    Join Date
    Jan 2007
    Posts
    143

    Re: union issues.

    I know this is a restriction....but i want to know why it has been restricted...the reason behind it...

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: union issues.

    Quote Originally Posted by resumurof View Post
    I know this is a restriction....but i want to know why it has been restricted...the reason behind it...
    Well, the answer is pretty apparent. Union is a way for interpreting the same memory region, therefore there should be the only and default way for constructing it.
    Best regards,
    Igor

  6. #6
    Join Date
    Aug 2008
    Posts
    902

    Re: union issues.

    In C++0x, the union example you posted is not restricted, since vehicle is a POD by C++0x standards.

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