[lug] Can't create union with classes that have copy CTOR's?

Scott A. Herod herod at interact-tv.com
Fri Mar 22 13:38:27 MST 2002


Last comment, I promise.  You can have unions of pointers.

#include <iostream>

class A {
public:
    A() { _a = 5; }
    A( A& a ) : _a(a._a) {};
    int _a;
};

class B {
public:
    B() { _b = 7; }
    B( B& b ) : _b(b._b) {};
    int _b;
};

union _spam { A* a; B* b; } spam;

int main() {
    A* a = new A();    cerr << "A: " << a->_a << endl;

    B* b = new B();    cerr << "B: " << b->_b << endl;

    B bee( *b );    cerr << "Bee: " << bee._b << endl;

    b->_b = 3;
    cerr << "B: " << b->_b << endl;
    cerr << "Bee: " << bee._b << endl;

    spam.b = b;    cerr << "Spammer: " << spam.b->_b << endl;
}


LittleViggy at alum.manhattan.edu wrote:
>
> Hey all,
>
> I'm trying to create a union of 5 classes.  All five have copy constructors,
> and the compiler is erroring out.  Albeit, I'm using Microsoft (sorry), but
> there's nothing in their documentation that says this is specific to M$.
>
> Is this ANSI complient?  You can't create a union of classes with copy CTOR's?
> If so, why not?
>
> Viggy



More information about the LUG mailing list