468,456 Members | 1,580 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,456 developers. It's quick & easy.

classes in unions?

Hi,

I was wandering if I can have classes in unions?

I basically have source code in a format very similar to:

union example {
ClassA variable1;
ClassB variable2;
};

class AnotherClass {
.....
example SomeName;
.....

};

However, the compiler (gcc) tells me constructors aren't allowed in
unions.

I have a very large program written for eg ClassA, and I am adding a
second class, eg ClassB, to it. I came to this particular code, and I
thought unions would be an easy solution, but it looks like I will have
to make other changes.

I thought since classes are types they would be allowed in unions.

so no classes in unions?

regrads

Jul 10 '06 #1
4 1353
ur*******@gmail.com wrote:
Hi,

I was wandering if I can have classes in unions?

I basically have source code in a format very similar to:

union example {
ClassA variable1;
ClassB variable2;
};

class AnotherClass {
.....
example SomeName;
.....

};

However, the compiler (gcc) tells me constructors aren't allowed in
unions.

I have a very large program written for eg ClassA, and I am adding a
second class, eg ClassB, to it. I came to this particular code, and I
thought unions would be an easy solution, but it looks like I will have
to make other changes.

I thought since classes are types they would be allowed in unions.

so no classes in unions?
Think about it - which constructor initialises the object?

Unions are rare in C++ (compared to C), C++ offers more appropriate
solutions. Unions are often used as a poor man's polymorphism in C.

--
Ian Collins.
Jul 10 '06 #2
ur*******@gmail.com posted:

I was wandering if I can have classes in unions?

Yes, but each member of the union must be a POD.

--

Frederick Gotham
Jul 10 '06 #3
ur*******@gmail.com wrote :
I was wandering if I can have classes in unions?
See boost.variant.
Jul 11 '06 #4
<ur*******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Hi,

I was wandering if I can have classes in unions?

I basically have source code in a format very similar to:

union example {
ClassA variable1;
ClassB variable2;
};

class AnotherClass {
....
example SomeName;
....

};

However, the compiler (gcc) tells me constructors aren't allowed in
unions.

I have a very large program written for eg ClassA, and I am adding a
second class, eg ClassB, to it. I came to this particular code, and I
thought unions would be an easy solution, but it looks like I will have
to make other changes.

I thought since classes are types they would be allowed in unions.

so no classes in unions?

regrads
Instead of a union, I think polymorphism would work better for you.
Jul 12 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by David | last post: by
6 posts views Thread by Neil Zanella | last post: by
16 posts views Thread by Tim Cambrant | last post: by
67 posts views Thread by bluejack | last post: by
11 posts views Thread by pereges | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.