Q:
Since struct and class are so similar, why does C++ have both?
23
C++ A Beginner’s Guide by Herbert Schildt
A:
On the surface, there is seeming redundancy in the fact that both structures and classes have
virtually identical capabilities. Many newcomers to C++ wonder why this apparent duplication exists. In
fact, it is not uncommon to hear the suggestion that either the keyword class or struct is unnecessary.
The answer to this line of reasoning is rooted in the desire to keep C++ compatible with C. As C++ is
currently defined, a standard C structure is also a completely valid C++ structure. In C, which has no
concept of public or private structure members, all structure members are public by default. This is why
members of C++ structures are public (rather than private) by default. Since the class keyword is
expressly designed to support encapsulation, it makes sense that its members are private by default.
Thus, to avoid incompatibility with C on this issue, the structure default could not be altered, so a new
keyword was added. However, in the long term, there is a more important reason for the separation of
structures and classes. Because class is an entity syntactically separate from struct, the definition of a
class is free to evolve in ways that may not be syntactically compatible with C-like structures. Since the
two are separated, the future direction of C++ will not be encumbered by concerns of compatibility with
C-like structures.
For the most part, C++ programmers will use a class to define the form of an object that contains
member functions and will use a struct in its more traditional role to create objects that contain only
data members. Sometimes the acronym “POD” is used to describe a structure that does not contain
member functions. It stands for “plain old data.”
Unions
A union is a memory location that is shared by two or more different variables. A union is created using
the keyword union, and its declaration is similar to that of a structure, as shown in this example:
union utype { short int i; char ch;
} ;
This defines a union in which a short int value and a char value share the same location. Be clear on one
point: It is not possible to have this union hold both an integer and a character at the same time,
because i and ch overlay each other. Instead, your program can treat the information in the union as an
integer or as a character at any time. Thus, a union gives you two or more ways to view the same piece
of data.
You can declare a union variable by placing its name at the end of the union declaration, or by using a
separate declaration statement. For example, to declare a union variable called u_var of type utype, you
would write
utype u_var;
In u_var, both the short integer i and the character ch share the same memory location. (Of course, i
occupies two bytes and ch uses only one.) Figure 9-1 shows how i and ch both share the same address.
24
C++ A Beginner’s Guide by Herbert Schildt
As far as C++ is concerned, a union is essentially a class in which all elements are stored in the same
location. In fact, a union defines a class type. A union can contain constructors and destructors as well as
member functions. Because the union is inherited from C, its members are public, not private, by
default.
Here is a program that uses a union to display the characters that comprise the low- and high-order
bytes of a short integer (assuming short integers are two bytes):
The output is shown here:
u as integer: 1000
u as chars: è
u2 as integer: 22872
u2 as chars: X Y
25
C++ A Beginner’s Guide by Herbert Schildt
As the output shows, using the u_type union, it is possible to view the same data two different ways.
Like the structure, the C++ union is derived from its C forerunner. However, in C, unions can include only
data members; functions and constructors are not allowed. In C++, the union has the expanded
capabilities of the class. But just because C++ gives unions greater power and flexibility does not mean
that you have to use it. Often unions contain only data. However, in cases where you can encapsulate a
union along with the routines that manipulate it, you will be adding considerable structure to your
program by doing so.
There are several restrictions that must be observed when you use C++ unions. Most of these have to do
with features of C++ that will be discussed later in this book, but they are mentioned here for
completeness. First, a union cannot inherit a class. Further, a union cannot be a base class. A union
cannot have virtual member functions. No static variables can be
members of a union. A reference member cannot be used. A union cannot have as a member any object
that overloads the = operator. Finally, no object can be a member of a union if the object has an explicit
constructor or destructor.
Anonymous Unions
There is a special type of union in C++ called an anonymous union. An anonymous union does not
include a type name, and no variables of the union can be declared. Instead, an anonymous union tells
the compiler that its member variables are to share the same location. However, the variables
themselves are referred to directly, without the normal dot operator syntax. For example, consider this
program:
26
C++ A Beginner’s Guide by Herbert Schildt
As you can see, the elements of the union are referenced as if they had been declared as normal local
variables. In fact, relative to your program, that is exactly how you will use them. Further, even though
they are defined within a union declaration, they are at the same scope level as any other local variable
within the same block. This implies that the names of the members of an anonymous union must not
conflict with other identifiers known within the same scope.
All restrictions involving unions apply to anonymous ones, with these additions. First, the only elements
contained within an anonymous union must be data. No member functions are allowed. Anonymous
unions cannot contain private or protected elements. (The protected specifier is discussed in Module
10.) Finally, global anonymous unions must be specified as static.
Do'stlaringiz bilan baham: |