niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » C++
classes in unions?
Post new topic   Reply to topic Page 1 of 1 [5 Posts] View previous topic :: View next topic
Author Message
uralmutlu@gmail.com
*nix forums beginner


Joined: 10 Jul 2006
Posts: 5

PostPosted: Mon Jul 10, 2006 9:21 pm    Post subject: classes in unions? Reply with 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
Back to top
Ian
*nix forums Guru


Joined: 16 Aug 2005
Posts: 1615

PostPosted: Mon Jul 10, 2006 9:52 pm    Post subject: Re: classes in unions? Reply with quote

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

PostPosted: Mon Jul 10, 2006 11:49 pm    Post subject: Re: classes in unions? Reply with quote

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

PostPosted: Tue Jul 11, 2006 1:54 am    Post subject: Re: classes in unions? Reply with quote

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

PostPosted: Wed Jul 12, 2006 11:50 am    Post subject: Re: classes in unions? Reply with quote

<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
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [5 Posts] View previous topic :: View next topic
The time now is Sun Nov 23, 2008 10:59 am | All times are GMT
navigation Forum index » Programming » C++
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Tagged unions johan.tibell@gmail.com C 1 Fri Jul 21, 2006 1:00 pm
No new posts Warning when new attributes are added to classes at run time Matthew Wilson python 7 Wed Jul 19, 2006 8:42 pm
No new posts Partial classes Sanjay python 26 Wed Jul 19, 2006 6:10 am
No new posts Which Advanced classes to take for oracle 10g OCM exam? gnu Server 0 Mon Jul 17, 2006 11:56 am
No new posts assignment operator for classes with const data scroopy C++ 19 Mon Jul 17, 2006 8:48 am

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
[ Time: 0.2220s ][ Queries: 16 (0.1380s) ][ GZIP on - Debug on ]