|
Data Types, Variables, and Constants |
|
|
|
|
Written by Hemanshu Patel
|
|
Friday, 19 October 2007 |
|
Page 5 of 8 | Programming Tip | In C++, it is common practice to use struct when creating C-style structures that include only data members. A class is usually reserved for creating classes that contain function members. Sometimes the acronym POD is used to describe a C-style structure. POD stands for Plain Old Data.> | There is a special type of union in C++ called an anonymous union. An anonymous union declaration does not contain a class name and no objects of that union are declared. Instead, an anonymous union simply tells the compiler that its member variables are to share the same memory location. However, the variables themselves are referred to directly, without using the normal dot or arrow operator syntax. The variables that make up an anonymous union are at the same scope level as any other variable declared within the same block. This implies that the union variable names must not conflict with any other names valid within their scope. For example, here is an anonymous union: union { // anonymous union int a; // a and f share float f; // the same memory location }; // ... a = 10; // access a cout << f; // access f Here, a and f both share the same memory location. As you can see, the names of the union variables are referred to directly without the use of the dot or arrow operator. All restrictions that apply to unions in general apply to anonymous unions. In addition, anonymous unions must contain only data—no member functions are allowed. Anonymous unions may not contain the private or protected keywords. Finally, an anonymous union with namespace scope must be declared as static. Enumerations Another type of variable that can be created is called an enumeration. An enumeration is a list of named integer constants. Thus, an enumeration type is simply a specification of the list of names that belong to the enumeration. To create an enumeration requires the use of the keyword enum. The general form of an enumeration type is enum enum-name { list of names } var-list; The enum-name is the enumeration’s type name. The list of names is comma separated. For example, the following fragment defines an enumeration of cities called cities and the variable c of type cities. Finally, c is assigned the value “Houston”. enum cities { Houston, Austin, Amarillo} c; c = Houston;> In an enumeration, the value of the first (leftmost) name is, by default, 0; the second name has the value 1; the third has the value 2; and so on. In general, each name is given a value one greater than the name that precedes it. You can give a name a specific value by adding an initializer. For example, in the following enumeration, Austin will have the value 10: enum cities { Houston, Austin=10, Amarillo }; In this example, Amarillo will have the value 11 because each name will be one greater than the one that precedes it. C Tags In C, the name of a structure, union, or enumeration does not define a complete type name. In C++, it does. For example, the following fragment is valid for C++, but not for C: struct s_type { int i; double d; }; // ... s_type x; // OK for C++, but not for C In C++, s_type defines a complete type name and can be used, by itself, to declare objects. In C, s_type defines a tag, which is not a complete type specifier. In C, you need to precede a tag name with either struct, union, or enum when declaring objects. For example, struct s_type x; // now OK for C The preceding syntax is also permissible in C++, but seldom used.
|
|
| |
|
|