What would be an effective and efficient way to tell which subvariable you want to use in a data structure like a union?

Hey, I have a union and it has a bunch of members in it. How can I tell for my functions which member of the union to use? Should I use an extra argument and have to manually do it... is there a function I can create that could tell which member in a union is valid.
I don't understand what you mean. Can you give a code example of what it is you'd like to do if the language allowed it?
Hmmm I guess I can. It doesn't show what I'm exactly trying to do... but it shows a little idk.. way I could do it.

It's actually really bad... and I'm going to rewrite.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
namespace 2de {
	namespace Collision {
	bool CollisionDetection(2de::Coordinates, 2de::Coordinates, int, int);
	bool CollisionDetection(std::vector<2de::Coordinates>, std::vector<2de::Coordinates>);
	bool CollisionDetection(2de::Object, 2de::Object);
	bool CollisionDetection(2de::Object, 2de::2deWindow);


	bool CollisionDetection(2de::Coordinates Blue, 2de::Coordinates Red, int pic ) {
		int hold;			
		if (pic == 0)
			 for(int i = 0; i < 4; i++){
				for(int j = 0; j < 4; j++){
					if (i == 0) {
						if (j == 0) {
							if ( ((Blue.Coord.x - Red.Coord.x) == .5) || ((Blue.Coord.x - Red.Coord.x) == -.5)
								return true;
						}
						if (j == 1) {
							if ( ((Blue.Coord.x - Red.Coord.y) == .5) || ((Blue.Coord.x - Red.Coord.y) == -.5))
								return true;
						}
				
						if(j == 2) {
							if ( ((Blue.Coord.x - Red.Coord.z) == .5) || ((Blue.Coord.x - Red.Coord.z) == -.5))
								return true;
						}
						if(j == 3) {
							if (  ((Blue.Coord.x - Red.Coord.w) == .5) || ((Blue.Coord.x - Red.Coord.w) == -.5))
								return true;
						}
					}
					
					if (i == 1) {
						if (j == 0) {
							if ( ((Blue.Coord.y - Red.Coord.x) == .5) || ((Blue.Coord.y - Red.Coord.x) == -.5)
								return true;
						}
						if (j == 1) {
							if ( ((Blue.Coord.y - Red.Coord.y) == .5) || ((Blue.Coord.y - Red.Coord.y) == -.5))
								return true;
						}
				
						if(j == 2) {
							if ( ((Blue.Coord.y - Red.Coord.z) == .5) || ((Blue.Coord.y - Red.Coord.z) == -.5))
								return true;
						}
						if(j == 3) {
							if (  ((Blue.Coord.y - Red.Coord.w) == .5) || ((Blue.Coord.y - Red.Coord.w) == -.5))
								return true;
						}
				}
 
					if (i == 2) {
						if (j == 0) {
							if ( ((Blue.Coord.z - Red.Coord.x) == .5) || ((Blue.Coord.z - Red.Coord.x) == -.5)
								return true;
						}
						if (j == 1) {
							if ( ((Blue.Coord.z - Red.Coord.y) == .5) || ((Blue.Coord.z - Red.Coord.y) == -.5))
								return true;
						}
				
						if(j == 2) {
							if ( ((Blue.Coord.z - Red.Coord.z) == .5) || ((Blue.Coord.z - Red.Coord.z) == -.5))
								return true;
						}
						if(j == 3) {
							if (  ((Blue.Coord.z - Red.Coord.w) == .5) || ((Blue.Coord.z - Red.Coord.w) == -.5))
								return true;
						}
 

	}

					
					if (i == 3) {
						if (j == 0) {
							if ( ((Blue.Coord.w - Red.Coord.x) == .5) || ((Blue.Coord.w - Red.Coord.x) == -.5)
								return true;
						}
						if (j == 1) {
							if ( ((Blue.Coord.w - Red.Coord.y) == .5) || ((Blue.Coord.w - Red.Coord.y) == -.5))
								return true;
						}
				
						if(j == 2) {
							if ( ((Blue.Coord.w - Red.Coord.z) == .5) || ((Blue.Coord.w - Red.Coord.z) == -.5))
								return true;
						}
						if(j == 3) {
							if (  ((Blue.Coord.w - Red.Coord.w) == .5) || ((Blue.Coord.w - Red.Coord.w) == -.5))
								return true;
						}
 

	}


	}
}


This code was really just a work around that I worked on yesterday. It's not done... It's actually quite bad... and I probably am going to recode it. I'm not using anything else than a union so don't suggest please... And that's all.

Also... I don't even know if that's really collision detection... So if you can see what i'm trying to do but not doing it... go ahead an explain a few things to me.
Last edited on
The structure will need a control variable (prob best to use a enum)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
enum MyType
{ typeInt
, typeDouble
, typeString };   
 
struct Example
{
    union {
        int i;
        double df;
        char* psz;
    }data; 

    MyType type;
}; 


Andy

P.S. I'm not sure I answered the question you meant to ask?
Last edited on
Hmmm I actually really like that solution.

Thanks. I actually thought about doing that... IDK maybe i'm trying to make impossible magical things happen. I was actually going to just have a class that kind of partners with any of my objects that will tell extra information with how it will, not be embedded within them.
Last edited on
I don't know enough about your problem to come up with any suggestions. Can you give an example of the kinds of classes and magical things you want to happen to them?

Andy

P.S. Are you familiar with polymorphism?
Last edited on
Yes, I'm familiar with polymorphism. I haven't really used it in any code to really know everything about it.

Basically, I wanted a union that had members... some mebers were actual other data structures. As you know you can only use one member of a union at a time because of same shared memory. I just needed a solution for a function to know which one of those members should of been used. My answer was to manually insert in integers that represented which member in the argument list of the functions were needed to test. Your solution on just embedding the union in a structure with extra variables telling what data to use was also useful. I think I'm going to use that... or just make an extra data structure that will guide the function in what members to use and actually work as a generic structure to all objects not just the union.
Last edited on
"My" idea was nicked from (Microsoft's) Automation VARIANT type/struct.

It's normally used, in C++ code, wrapped up in a little class. Microsoft provides three of these itself: COleVariant in MFC, CComVariant in ATL, and _variant_t in the Windows SDK; the latter is actually provided by Visual C++, as part if its COM support (the first two you have to pay for, but not the third).[/s]
Last edited on
Topic archived. No new replies allowed.