Archived

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

Greatwolf

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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites