You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the previous example, the union in the `Input` struct has no name, so it's called an *anonymous* union. Its members can be accessed directly as if they're members of the struct. For more information about how to use an anonymous union, see the [Anonymous union](#anonymous_union) section.
138
+
In the previous example, the union in the `Input` struct has no name, so it's called an *anonymous* union. Its members can be accessed directly as if they're members of the struct. For more information about how to use an anonymous union, see the [Anonymous union](#anonymous_unions) section.
139
139
140
140
The previous example shows a problem that you could also solve by using class types that derive from a common base class. You could branch your code based on the runtime type of each object in the container. Your code might be easier to maintain and understand, but it might also be slower than using a union. Also, with a union, you can store unrelated types. A union lets you dynamically change the type of the stored value without changing the type of the union variable itself. For example, you could create a heterogeneous array of `MyUnionType`, whose elements store different values of different types.
// must use placement new because of string member!
555
-
new(&temp) TempData(td);
556
-
}
557
-
558
-
TempData GetTemp()
559
-
{
560
-
if (type == WeatherDataType::Temperature)
561
-
return temp;
562
-
else
563
-
throw logic_error("Can't return TempData when Input holds a WindData");
564
-
}
565
-
void SetWind(WindData wd)
566
-
{
567
-
// Explicitly delete struct member that has a
568
-
// non-trivial constructor
569
-
if (type == WeatherDataType::Temperature)
570
-
{
571
-
temp.StationId.~string();
572
-
}
573
-
wind = wd; //placement new not required.
574
-
}
575
-
WindData GetWind()
576
-
{
577
-
if (type == WeatherDataType::Wind)
578
-
{
579
-
return wind;
580
-
}
581
-
else
582
-
throw logic_error("Can't return WindData when Input holds a TempData");
583
-
}
584
-
585
-
private:
586
-
587
-
union
588
-
{
589
-
TempData temp;
590
-
WindData wind;
591
-
};
592
-
};
593
-
```
594
-
595
506
A union can't store a reference. A union also doesn’t support inheritance. That means you can't use a union as a base class, or inherit from another class, or have virtual functions.
596
507
597
508
## Initialize a union
@@ -627,27 +538,25 @@ The `NumericType` union is arranged in memory (conceptually) as shown in the fol
627
538
 <br/>
628
539
Storage of data in a `NumericType` union
629
540
630
-
## <aname="anonymous_union"></a> Anonymous union
541
+
## <aname="anonymous_unions"></a> Anonymous union
631
542
632
543
An anonymous union is one declared without a *`class-name`* or *`declarator-list`*.
633
544
634
-
```cpp
635
-
union { member-list }
636
-
```
545
+
> **`union {`***`member-list`***`}`**
637
546
638
547
Names declared in an anonymous union are used directly, like nonmember variables. It implies that the names declared in an anonymous union must be unique in the surrounding scope.
639
548
640
549
An anonymous union is subject to these additional restrictions:
641
550
642
551
- If declared in file or namespace scope, it must also be declared as **`static`**.
643
552
644
-
- It can have only **`public`** members; **`private`** and **`protected`** members in an anonymous union generates errors.
553
+
- It can have only **`public`** members; having **`private`** and **`protected`** members in an anonymous union generates errors.
645
554
646
555
- It can't have member functions.
647
556
648
557
## See also
649
558
650
559
[Classes and Structs](../cpp/classes-and-structs-cpp.md)<br/>
0 commit comments