classes, structs and unions

This is a discussion on classes, structs and unions within the C++ Programming forums, part of the General Programming Boards category; I found unions to be useful when dealing whith 128 bit data (used when working with sse) Code: union { ...

  1. #16
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    I found unions to be useful when dealing whith 128 bit data (used when working with sse)

    Code:
    union
    {
       _m128 sseData;
       float normalData[4];
    }

  2. #17

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    You can also use unions to initialize arrays within classes/structs.

  3. #18
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by rudyman View Post
    You can also use unions to initialize arrays within classes/structs.
    Do you have an example of that?
    I've never found any need to use a union, ever; so I'm wondering if you're talking about a unique case I haven't encountered yet, or if there's a better way than using a union?

  4. #19

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    This is how I do it, at least:

    Code:
    class MyClass
    {
    public:
          union
          {
                const int array [3];                          // These both have the
                struct { const int array0, array1, array2; }; // same memory layout.
          };
    
          MyClass (int val0, int val1, int val2)
                : array(), array0 (val0), array1 (val1), array2 (val2)
          {
          }
    };
    
    MyClass object (3, 4, 5);
    // object.array[0] = 3, object.array[1] = 4, object.array[2] = 5
    I think it only works with POD types, though.
    Last edited by rudyman; 08-08-2008 at 07:48 PM.

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Well, that one is pretty useless. You have your array. Why re-name the elements in the array to arrayN?
    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.

  6. #21
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    To get access to the array structure, but that can't be much better than the copy algorithm. I would avoid this kind of premature pessimism, unless you absolutely cannot afford the temporary array, and in that case I would want a completely different object design.

    Code:
    #include <algorithm>
    
    namespace {
        template<class T, std::size_t N>
        class Foo 
        {
        public:
            Foo (const T * elems, std::size_t elemssize)
            {
                if (elemssize <= N)
                    std::copy(elems, elems + elemssize, _elems);
            }
    
            ~Foo ()
            {
                //nothing
            }
    
        private:
            T _elems[N];
        };
    }
    
    int main ()
    {
        const std::size_t barsize = 5;
        int * bar = new int [barsize];
    
        for (int i = 0; i < barsize; i++) {
            bar[i] = i * 2 + 1;
        }
        
        Foo<int, barsize> bomb(bar, barsize);
        delete [] bar;
    
        // use bomb ...
    }
    Last edited by whiteflags; 08-09-2008 at 09:07 AM.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Yes, there's no need for the union in the first place. You could merely place the array outside, because it really doesn't do much being placed inside there...
    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.

  8. #23

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    Quote Originally Posted by Elysia View Post
    Yes, there's no need for the union in the first place. You could merely place the array outside, because it really doesn't do much being placed inside there...
    But without the union, how could you initialize the array and keep it encapsulated?

  9. #24
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    My example does not break encapsulation: an object simply takes ownership of another object, namely the array is copied into Foo and then blips out of existence. There are neater ways to do that, but it's certainly ok.

    Still, I'm not impressed by your reasoning. Thinking about it some more, std::fill or std::generate could probably do the job most of the time if you really depended on certain array values for your object and had no other array to copy from.

    If that isn't a good decision, then I'd make an init method of some sort, which also solves the problem of encapsulation.

    Otherwise, just make an assign method or something like that and wait until the values are really needed. That makes the underlying array structure a more dynamic one as well, allowing you to determine when it can extend and shrink.
    Last edited by whiteflags; 08-09-2008 at 11:30 AM.

  10. #25
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by rudyman View Post
    Code:
     const int array [3];                          // These both have the
                struct { const int array0, array1, array2; }; // same memory layout.
    I don't think that's guaranteed.

Page 2 of 2 FirstFirst 12
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, 11: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21