Unions

Apr 21, 2011 at 1:19pm
I want some example on unions.
How can i create union with switch_type attribute.
And how can i initialize that variable.
i want this program in c++
Thank you
Apr 21, 2011 at 6:22pm
Here for you:
http://www.cplusplus.com/doc/tutorial/other_data_types/

If I understand correctly, the "switch type" you want is some information about the way union's data should be interpreted or which member should be taken in account. If this is you're after, then declare a union inside a structure that have a separate member (not affected by union) that holds that information. I would give here an example of it but I don't understand the next of your requirement - "i want this program in c++". The piece of code I was about to put here I must admit - doesn't have nothing specific to C++.
...or do I had to replace the "struct" I've mention above with "class"?
Apr 22, 2011 at 4:24am
Thank you to gave replay.....

I have code like this..
typedef [switch_type(int)] union {
[case(0)] int red;
[case(1)] int green;
[case(2)] int blue;
} colorType;

I want create one variable of above union and assign some value.
I don't know how to create variable please help me.
Apr 22, 2011 at 4:58am
what do you mean by switch_type?

from the looks of it, you might just want a struct:

1
2
3
4
5
6
struct colorType
{
  int red;
  int green;
  int blue;
};


Are you sure you want a union here? Why?
Apr 22, 2011 at 5:12am
Actually i got this piece of code from one idl file.
i didn't understand that.
I am asking you to help me to understand that code
Apr 22, 2011 at 5:50am
IDL = Interface Description Language

IDL files are to do with COM programming - If you are using that IDL file to learn what unions are
then you are a long long way off target.
Topic archived. No new replies allowed.