By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,897 Members | 1,955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,897 IT Pros & Developers. It's quick & easy.

classes in unions?

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
ur*******@gmail.com wrote :
I was wandering if I can have classes in unions?
See boost.variant.
Jul 11 '06 #4

P: n/a
<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.