472,782 Members | 933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,782 software developers and data experts.

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 1690
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: David | last post by:
Some developers in my group are using UNIONS to define their data types in a C++ program for an embedded system. Are there any pro and cons in doing this when you can define a CLASS to do the same...
6
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
16
by: Tim Cambrant | last post by:
Hi. I was reading up a bit on the features of C I seldom use, and I came across unions. I understand the concept, and that all the contained variables etc. share the same memory. Thus, when a new...
23
by: rohit | last post by:
Hi, In my couple of years of experience, I have never found a single instance where I needed to use unions and bitfields(though I have used structures).I was just imagining where would these find...
67
by: bluejack | last post by:
A recent post asking for help with unions reminded me of this component of the C language that I have only used a couple of times, and those almost entirely out of personal whim -- Unions for the...
11
by: pereges | last post by:
Hello, can some one please guide me a little into using unions. I read about unions in K & R but I am finding it difficult to apply to my problem at hand. I want to save up some space by using...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.