Archived

This topic is now archived and is closed to further replies.

Can an Object be inside a UNION?

Recommended Posts

I ask this because my compiler is complaining when I do something like this:
struct CInferenceEngine::TokenType
{
bool isOp;//tells me which one''s active

union
{
conditionType condition;
apstring op;
};//union

};//TokenType


error I get: ! Error E2456 CInferenceEngine.cpp 12: Union member ''CInferenceEngine::TokenType:: ::op'' is of type class with constructor I went back and looked through some of my books to see if there was any such limitation but I can''t seem to find any mention of it. Thanks

You fight like a dairy farmer.

Share on other sites
A union cannot have data members of any type that have a non-trivial default constructor (ie. one you define explicitly).

[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people

[edited by - wild_pointer on July 5, 2003 4:22:33 AM]

Share on other sites
but but... that sucks!

You fight like a dairy farmer.

Share on other sites
What are you trying to do with a union of two classes anyway?

Share on other sites
It's wierd isn't it? A feature of c++ that I thought I would never have any use for somehow mysteriously found it's way in my code lol...

Well basically I'm using this type in my parser. The line that's read in from the file is made up of a series of premise and logical operators. If you think of a math equation it's made up of constants, variables etc and operators, so it's kind of like that expect it's for preposition logic rules.

After reading the rule in, I decided I wanted the representation of this rule to be store in a vector and each element in the vector could either be a logical operator(and, or, implication, etc.) and then either a premise or a conclusion. Obviously if an element, or I call them tokens here, is a logical operator then it obviously isn't a premise or conclusion. Just like in a math equation a constant or variable clearly isn't an operator.

If you take a look at my Wumpus post over here it'll give you an idea of what I'm currently working on.

Thanks

You fight like a dairy farmer.

[edited by - Greatwolf on July 5, 2003 2:26:24 PM]

Share on other sites
That could probably better be solved with inheritance. Derive each type of operation from a common base class, token perhaps? Then store a list of those instead.

[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people

Share on other sites
Hmm, I''m not sure I see how that works. Can you please elaborate? Maybe some diagrams to illustrate what you mean will go a long way.

Thanks

You fight like a dairy farmer.

Share on other sites
Try storing a pointer to each class in the union instead. I did a small test and it seemed to work.

First make it work,
then make it fast.

--Brian Kernighan

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book

• Forum Statistics

• Total Topics
628354
• Total Posts
2982243

• 10
• 9
• 11
• 24
• 11