union questions

Hi,

I'm new to unions, made a little toy program that brought up some questions.

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
#include <stdlib.h>
class Foo{};
class Bar{};
class Baz{};
class Filter
{
public:
	union FilterType // can union be private?
	{
		Foo* foo;
		Bar* bar;
		Baz* baz;
	} filteringFor;
	void setFilterFoo(Foo* f)  { filteringFor.foo = f; }
	void setFilterBar(Bar* br) { filteringFor.bar = br; }
	void setFilterBaz(Baz* bz) { filteringFor.baz = bz; }
	Filter::FilterType getFilteringFor(){ return filteringFor;}
	
};

int main(int argc, char *argv[])
{
	Foo* f = new Foo();
	Bar* br = new Bar();
	Baz* bz = new Baz();
	Filter fil = Filter();
	switch (rand()%3)
	{
	case 0:
	//fil.setFilterFoo(f);
		break;
	case 1:
		fil.setFilterBar(br);
		break;
	case 2:
		fil.setFilterBar(br);
		break;
	default:
		break;
	}
	//The parts I'm interested in
	if (fil.getFilteringFor().foo){ return 0; } // how does this equal true?
    if (fil.getFilteringFor().bar == br){return 1;} // it works
	if (fil.getFilteringFor().baz) { return 2; } // how does this equal true??
	else return -1;
		
}

Can unions be private? I know it's silly just asking because my compiler told me Filter::FilterType was undefined when I tried to.
1
2
public:
	union FilterType // can union be private? 

Do these equate to true because all members of union point to the same location regardless of that location's type?
1
2
if (fil.getFilteringFor().foo)
if (fil.getFilteringFor().baz)


Thanks in advance
Last edited on
> Can unions be private?

Yes.

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
struct A
{
    // both type and member object are public
    public: union int_or_double { int i ; double d ; /* ... */ };
    public: int_or_double i_or_d = {0} ;
};

struct B
{
    // type is public, member object is private
    public: union int_or_double { int i ; double d ; /* ... */ };
    public: int_or_double foo() const { return i_or_d ; } ;

    private: int_or_double i_or_d = {0} ;
};

struct C
{
    // both type and member object are private
    private: union int_or_double { int i ; double d ; /* ... */ };
    private: int_or_double i_or_d = {0} ;
};

int main()
{
    A a ;
    A::int_or_double v1 = a.i_or_d ; // fine

    B b ;
    B::int_or_double v2 = b.foo() ; // fine'
    B::int_or_double v3 = b.i_or_d ; // ***error: member B::i_or_d is private ;

    C::int_or_double v4 ; // ***error: type C::int_or_double is private ;
}



> Do these equate to true because all members of union point to the same location regardless of that location's type?

it's undefined behavior to read from the member of the union that wasn't most recently written. Many compilers implement, as a non-standard language extension, the ability to read inactive members of a union.
http://en.cppreference.com/w/cpp/language/union


Strongly favour union-like classes. See 'Union-like classes' in the page linked to earlier.
Topic archived. No new replies allowed.