|
|
|
|
|
|
| Author |
Message |
uralmutlu@gmail.com *nix forums beginner
Joined: 10 Jul 2006
Posts: 5
|
Posted: Mon Jul 10, 2006 9:21 pm Post subject:
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 |
|
| Back to top |
|
 |
Ian *nix forums Guru
Joined: 16 Aug 2005
Posts: 1615
|
Posted: Mon Jul 10, 2006 9:52 pm Post subject:
Re: classes in unions?
|
|
|
uralmutlu@gmail.com wrote:
| Quote: | 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. |
|
| Back to top |
|
 |
Frederick Gotham *nix forums Guru
Joined: 09 Jun 2006
Posts: 502
|
Posted: Mon Jul 10, 2006 11:49 pm Post subject:
Re: classes in unions?
|
|
|
uralmutlu@gmail.com posted:
| Quote: | I was wandering if I can have classes in unions?
|
Yes, but each member of the union must be a POD.
--
Frederick Gotham |
|
| Back to top |
|
 |
loufoque *nix forums addict
Joined: 27 Jan 2006
Posts: 97
|
Posted: Tue Jul 11, 2006 1:54 am Post subject:
Re: classes in unions?
|
|
|
uralmutlu@gmail.com wrote :
| Quote: | I was wandering if I can have classes in unions?
|
See boost.variant. |
|
| Back to top |
|
 |
Jim Langston *nix forums Guru
Joined: 15 Jun 2005
Posts: 649
|
Posted: Wed Jul 12, 2006 11:50 am Post subject:
Re: classes in unions?
|
|
|
<uralmutlu@gmail.com> wrote in message
news:1152566495.173942.262310@b28g2000cwb.googlegroups.com...
| Quote: | 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. |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|
|
The time now is Sun Nov 23, 2008 10:59 am | All times are GMT
|
|
Credit Card | Mobile Phones | MPAA | Mortgage Calculator | Flights to Bangkok
|
|
Copyright © 2004-2005 DeniX Solutions SRL
|
|
|
|
Other DeniX Solutions sites:
Unix/Linux blog |
electronics forum |
medicine forum |
science forum |
|
|
Privacy Policy
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|