Skip to content

Commit cbbb3c8

Browse files
author
Colin Robertson
committed
Fix link issue, update code samples
1 parent 5a2816c commit cbbb3c8

File tree

1 file changed

+26
-117
lines changed

1 file changed

+26
-117
lines changed

docs/cpp/unions.md

Lines changed: 26 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ ms.date: "08/18/2020"
44
f1_keywords: ["union_cpp"]
55
helpviewer_keywords: ["class type [C++], union as", "union keyword [C++]"]
66
ms.assetid: 25c4e219-fcbb-4b7b-9b64-83f3252a92ca
7-
no-loc: ["union", "struct", "enum", "class"]
7+
no-loc: ["union", "struct", "enum", "class", "static"]
88
---
99
# `union`
1010

@@ -17,11 +17,11 @@ A union can be useful for conserving memory when you have lots of objects and li
1717

1818
## Syntax
1919

20-
> **`union`** *`class-name`*<sub>opt</sub> **`{`** *`member-list`* **`};`**
20+
> **`union`** *`tag`*<sub>opt</sub> **`{`** *`member-list`* **`};`**
2121
2222
### Parameters
2323

24-
*`class-name`*<br/>
24+
*`tag`*<br/>
2525
The type name given to the union.
2626

2727
*`member-list`*<br/>
@@ -42,6 +42,7 @@ union RecordType // Declare a simple union type
4242
double d;
4343
int *int_ptr;
4444
};
45+
4546
int main()
4647
{
4748
RecordType t;
@@ -95,16 +96,27 @@ struct Input
9596
void Process_Temp(TempData t) {}
9697
void Process_Wind(WindData w) {}
9798

98-
// Container for all the data records
99-
queue<Input> inputs;
100-
void Initialize();
99+
void Initialize(std::queue<Input>& inputs)
100+
{
101+
Input first;
102+
first.type = WeatherDataType::Temperature;
103+
first.temp = { 101, 1418855664, 91.8, 108.5, 67.2 };
104+
inputs.push(first);
105+
106+
Input second;
107+
second.type = WeatherDataType::Wind;
108+
second.wind = { 204, 1418859354, 14, 27 };
109+
inputs.push(second);
110+
}
101111

102112
int main(int argc, char* argv[])
103113
{
104-
Initialize();
114+
// Container for all the data records
115+
queue<Input> inputs;
116+
Initialize(inputs);
105117
while (!inputs.empty())
106118
{
107-
Input i = inputs.front();
119+
Input const i = inputs.front();
108120
switch (i.type)
109121
{
110122
case WeatherDataType::Temperature:
@@ -121,21 +133,9 @@ int main(int argc, char* argv[])
121133
}
122134
return 0;
123135
}
124-
125-
void Initialize()
126-
{
127-
Input first, second;
128-
first.type = WeatherDataType::Temperature;
129-
first.temp = { 101, 1418855664, 91.8, 108.5, 67.2 };
130-
inputs.push(first);
131-
132-
second.type = WeatherDataType::Wind;
133-
second.wind = { 204,1418859354, 14, 27 };
134-
inputs.push(second);
135-
}
136136
```
137137
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_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.
139139
140140
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.
141141
@@ -503,95 +503,6 @@ int main()
503503
}
504504
```
505505

506-
```cpp
507-
#include <queue>
508-
#include <iostream>
509-
using namespace std;
510-
511-
enum class WeatherDataType
512-
{
513-
Temperature, Wind
514-
};
515-
516-
struct TempData
517-
{
518-
TempData() : StationId(""), time(0), current(0), maxTemp(0), minTemp(0) {}
519-
TempData(string id, time_t t, double cur, double max, double min)
520-
: StationId(id), time(t), current(cur), maxTemp(max), minTemp(0) {}
521-
string StationId;
522-
time_t time = 0;
523-
double current;
524-
double maxTemp;
525-
double minTemp;
526-
};
527-
528-
struct WindData
529-
{
530-
int StationId;
531-
time_t time;
532-
int speed;
533-
short direction;
534-
};
535-
536-
struct Input
537-
{
538-
Input() {}
539-
Input(const Input&) {}
540-
541-
~Input()
542-
{
543-
if (type == WeatherDataType::Temperature)
544-
{
545-
temp.StationId.~string();
546-
}
547-
}
548-
549-
WeatherDataType type;
550-
void SetTemp(const TempData& td)
551-
{
552-
type = WeatherDataType::Temperature;
553-
554-
// 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-
595506
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.
596507

597508
## Initialize a union
@@ -627,27 +538,25 @@ The `NumericType` union is arranged in memory (conceptually) as shown in the fol
627538
![Storage of data in a numeric type union](../cpp/media/vc38ul1.png "Storage of data in a NumericType union") <br/>
628539
Storage of data in a `NumericType` union
629540

630-
## <a name="anonymous_union"></a> Anonymous union
541+
## <a name="anonymous_unions"></a> Anonymous union
631542

632543
An anonymous union is one declared without a *`class-name`* or *`declarator-list`*.
633544

634-
```cpp
635-
union { member-list }
636-
```
545+
> **`union {`** *`member-list`* **`}`**
637546
638547
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.
639548

640549
An anonymous union is subject to these additional restrictions:
641550

642551
- If declared in file or namespace scope, it must also be declared as **`static`**.
643552

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.
645554

646555
- It can't have member functions.
647556

648557
## See also
649558

650559
[Classes and Structs](../cpp/classes-and-structs-cpp.md)<br/>
651560
[Keywords](../cpp/keywords-cpp.md)<br/>
652-
[class](../cpp/class-cpp.md)<br/>
653-
[struct](../cpp/struct-cpp.md)
561+
[`class`](../cpp/class-cpp.md)<br/>
562+
[`struct`](../cpp/struct-cpp.md)

0 commit comments

Comments
 (0)