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 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. 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 ur*******@gmail.com wrote :
I was wandering if I can have classes in unions?
See boost.variant.
<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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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;
};
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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=()=>{
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
| |