union with class

This is a discussion on union with class within the C++ Programming forums, part of the General Programming Boards category; is it possible to have unions with classes in them? Like this: Code: union { ClassNameA a; ClassNameB b; } ...

  1. #1
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297

    union with class

    is it possible to have unions with classes in them? Like this:
    Code:
    union
         {
         ClassNameA a;
         ClassNameB b;
         } unionname;
    and if so, can constructors and destructors of a and b get called? Or do you guys feel that one should stay away from unions in the world of C++.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Try the following

    Code:
    #include <iostream>
    using namespace std;
    
    class ClassNameA{
    public:
    //	ClassNameA(){cout << "ClassNameA constructor" << endl;}
    	void Sing(){cout << "ClassNameA sings \"Lalalala\"" << endl;}
    };
    class ClassNameB{
    public:
    //	ClassNameB(){cout << "ClassNameB constructor" << endl;}
    	void Sing(){cout << "ClassNameB sings \"Lalalala\"" << endl;}
    };
    
    union
         {
         ClassNameA a;
         ClassNameB b;
         } unionname;
    
    int main(void) {
    
    	unionname.a.Sing();
    
    	unionname.b.Sing();
    
    return 0;
    }
    Worked ok as it was, but as soon as you uncomment the constructors, the compiler throws a wobbly.......It seems to only want to create enough space for the union without calling constructors...makes sense as which constructor should be called for that 1 chunk of allocated memory!!!

    I wouldnt want to declare my classes like that anyway... remember that unions are just the compiler's way of addressing and working with a region in memory in different ways.....so with all the oop options C++ offers you can probably do what you wanted to do using inheritance etc...

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I know I know. But the project is a C to C++ conversion and there is a lot of union code in there. It would have been nice to be able to do that. In either case, it was an obscure part of C++ that I did not know.

    I also did some research into unions in C++ and found that they can have member functions including constructors and destructors. They can also have private and public members. Man, that's freaky!
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Originally posted by FillYourBrain
    I also did some research into unions in C++ and found that they can have member functions including constructors and destructors. They can also have private and public members. Man, that's freaky!
    Hehe...yeah it does seem a little freaky.....but when you look think about it, the idea that a function is part of a class or struct or union is just the way the compiler has you looking at it.....in reality the member functions are just functions & the way they are linked to a class is notional.....

    Cool though

  5. #5
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by FillYourBrain
    I know I know. But the project is a C to C++ conversion and there is a lot of union code in there. It would have been nice to be able to do that. In either case, it was an obscure part of C++ that I did not know.

    I also did some research into unions in C++ and found that they can have member functions including constructors and destructors. They can also have private and public members. Man, that's freaky!
    why are you converting it? if it works fine, leave well enough alone.
    hello, internet!

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Originally posted by moi
    why are you converting it? if it works fine, leave well enough alone.
    for the sake of structure. For the sake of improvement of the product. Feature addition, etc...

    C coders (at least here) have in the past written some pretty interwoven code. extern keywords and reuse of global variables everywhere. All functions have side effects. This causes confusion and makes a not so complex product into something complex. Since the job is to make it very structured and well documented it only makes sense to use classes along the way.

    "leave well enough alone" is the attitude that some of the guys here might take too. But at the same time, they spend most of their day debugging and reintroducing bugs that are only there because the product is sloppy.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    That'd be pretty ballsy to try FYB. You'd be using the same memory for two totally diferent things. Just seems to me like one of those things you might be able to get away with, but you wouldn't want to use.

  8. #8
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by FillYourBrain

    for the sake of structure. For the sake of improvement of the product. Feature addition, etc...

    C coders (at least here) have in the past written some pretty interwoven code. extern keywords and reuse of global variables everywhere. All functions have side effects. This causes confusion and makes a not so complex product into something complex. Since the job is to make it very structured and well documented it only makes sense to use classes along the way.

    "leave well enough alone" is the attitude that some of the guys here might take too. But at the same time, they spend most of their day debugging and reintroducing bugs that are only there because the product is sloppy.
    ohh, ok if you're going to make further improvements on it then yeah. at first i thought you were just converting for the sake of converting
    hello, internet!

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >is it possible to have unions with classes in them?
    Of course, but you have some restrictions that make such a union impractical. You cannot have objects with constructors or destructors as union members because it isn't possible to guarantee that the correct constructor or destructor would be called. Unions also cannot have any static members for the obvious reasons.

    -Prelude
    My best code is written with the delete key.

  10. #10
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Originally posted by Prelude
    >is it possible to have unions with classes in them?
    Of course, but you have some restrictions that make such a union impractical. You cannot have objects with constructors or destructors as union members because it isn't possible to guarantee that the correct constructor or destructor would be called. Unions also cannot have any static members for the obvious reasons.

    -Prelude
    Basically this is what The C++ Programming Language says, but it also mentions to use inheritance instead of unions. Try to figure that one out. lol

  11. #11
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    technically you can use inheritance instead of unions, but it would require allocations. a union allows you to use the same memory repeatedly for different types. BIG speed difference if you're doing something in a loop.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  12. #12
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Use polymorphism.

  13. #13
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Originally posted by Troll_King
    Use polymorphism.
    uh, what did I just say? Polymorphism is the reason you would use inheritance.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  14. #14
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    I meant use polygamy instead of polymorphism. Or sorry that didn't make it into the standard yet. Not until 2003!

  15. #15
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Originally posted by Troll_King
    I meant use polygamy instead of polymorphism. Or sorry that didn't make it into the standard yet. Not until 2003!
    Look here

    Since when has marital status been important in code production....and wouldnt having more than 1 spouse leave even less time for coding


Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  3. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 10:33 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

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