The free union Of C++

Examine the program named UNIONEX.CPP for an example of a free union.

In ANSI-C, all unions must be named in order to be used, but this is not true in C++. When using C++ we can use a free union, a union without a name. The union is embedded within a simple structure and you will notice that there is not a variable name following the declaration of the union in line 11. In ANSI-C, we would have to name the union and give a triple name (three names dotted together) to access the members. Since it is a free union, there is no union name, and the variables are accessed with only a doubly dotted name as illustrated in lines 18, 22, 26, 28, and 29.

You will recall that a union causes all the data contained within the union to be stored in the same physical memory locations, such that only one variable is actually available at a time. This is exactly what is happening here. The variable named fuel_load, bomb_load, and pallets are stored in the same physical memory locations and it is up to the programmer to keep track of which variable is stored there at any given time. You will notice that the transport is assigned a value for pallets in line 26, then a value for fuel_load in line 28. When the value for fuel_load is assigned, the value for pallets is corrupted and is no longer available since it was stored where fuel_load is currently stored. The observant student will notice that this is exactly the way the union is used in ANSI-C except for the way components are named.

The remainder of the program should be easy for you to understand, so after you study it and understand it, compile and execute it.

C++ Type conversions

Examine the program named TYPECONV.CPP for a few examples of type conversions in C++.

The type conversions are done in C++ in exactly the same manner as they are done in ANSI-C, but C++ gives you another form for doing the conversions.

Lines 10 through 18 of this program use the familiar “cast” form of type conversions used in ANSI-C, and there is nothing new to the experienced C programmer. You will notice that lines 10 through 13 are actually all identical to each other. The only difference is that we are coercing the compiler to do the indicated type conversions prior to doing the addition and the assignment in some of the statements. In line 13, the int type variable will be converted to type float prior to the addition, then the resulting float will be converted to type char prior to being assigned to the variable c.

Additional examples of type coercion are given in lines 15 through 18 and all four of these lines are essentially the same.

he examples given in lines 20 through 28 are unique to C++ and are not valid in ANSI-C. In these lines the type coercions are written as though they are function calls instead of the “cast” method as illustrated earlier. Lines 20 through 28 are identical to lines 10 through 18.

You may find this method of type coercion to be clearer and easier to understand than the “cast” method and in C++ you are free to use either, or to mix them if you so desire, but your code could be very difficult to read if you indescriminantly mix them.

Be sure to compile and execute this example program.

Share
Tweet
Share
Pin