C++ allows the definition of our own types based on other existing data types. We can do this using the keyword
typedef, whose format is:
typedef existing_type new_type_name ;
where
existing_typeis a C++ fundamental or compound type and
new_type_nameis the name for the new type we are defining. For example:
1
2
3
4
typedef char C;
typedef unsigned int WORD;
typedef char * pChar;
typedef char field [50];
In this case we have defined four data types:
C,
WORD,
pCharand
fieldas
char,
unsigned int,
char*and
char[50]respectively, that we could perfectly use in declarations later as any other valid type:
1
2
3
4
C mychar, anotherchar, *ptc1;
WORD myword;
pChar ptc2;
field name;
does not create different types. It only creates synonyms of existing types. That means that the type of
mywordcan be considered to be either
WORDor
unsigned int, since both are in fact the same type.
typedef can be useful to define an alias for a type that is frequently used within a program. It is also useful to define types when it is possible that we will need to change the type in later versions of our program, or if a type you want to use has a name that is too long or confusing.
Unions Unions allow one same portion of memory to be accessed as different data types, since all of them are in fact the same location in memory. Its declaration and use is similar to the one of structures but its functionality is totally different:union union_name {
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
} object_names;
All the elements of the
uniondeclaration occupy the same physical space in memory. Its size is the one of the greatest element of the declaration. For example:
1
2
3
4
5
union mytypes_t {
char c;
int i;
float f;
} mytypes;
defines three elements:
1
2
3
mytypes.c
mytypes.i
mytypes.
each one with a different data type. Since all of them are referring to the same location in memory, the modification of one of the elements will affect the value of all of them. We cannot store different values in them independent of each other.
One of the uses a union may have is to unite an elementary type with an array or structures of smaller elements. For example:
1
2
3
4
5
6
7
8
union mix_t {
long l;
struct {
short hi;
short lo;
} s;
char c[4];
} mix;
defines three names that allow us to access the same group of 4 bytes:
mix.l,
mix.sand
mix.cand which we can use according to how we want to access these bytes, as if they were a single
long-type data, as if they were two
shortelements or as an array of
charelements, respectively. I have mixed types, arrays and structures in the union so that you can see the different ways that we can access the data. For a
little-endiansystem (most PC platforms), this union could be represented as:
The exact alignment and order of the members of a union in memory is platform dependent. Therefore be aware of possible portability issues with this type of use.
Anonymous unions In C++ we have the option to declare anonymous unions. If we declare a union without any name, the union will be anonymous and we will be able to access its members directly by their member names. For example, look at the difference between these two structure declarations: structure with regular union structure with anonymous unionstruct {
char title[50];
char author[50];
union {
float dollars;
int yen;
} price;
} book;
struct {
char title[50];
char author[50];
union {
float dollars;
int yen;
};
} book;
The only difference between the two pieces of code is that in the first one we have given a name to the union (
price) and in the second one we have not. The difference is seen when we access the members
dollarsand
yenof an object of this type. For an object of the first type, it would be:
1
2
book.price.dollars
book.price.
whereas for an object of the second type, it would be:
Once again I remind you that because it is a union and not a struct, the members
dollarsand
yenoccupy the same physical space in the memory so they cannot be used to store two different values simultaneously. You can set a value for
pricein dollars or in yen, but not in both.
Enumerations (enum) Enumerations create new data types to contain something different that is not limited to the values fundamental data types may take. Its form is the following:enum enumeration_name {
value1,
value2,
value3,
.
.
} object_names;
For example, we could create a new type of variable called
colors_tto store colors with the following declaration:
1
enum colors_t {black, blue, green, cyan, red, purple, yellow, white};
Notice that we do not include any fundamental data type in the declaration. To say it somehow, we have created a whole new data type from scratch without basing it on any other existing type. The possible values that variables of this new type
color_tmay take are the new constant values included within braces. For example, once the
colors_tenumeration is declared the following expressions will be valid:
1
2
3
4
colors_t mycolor;
mycolor = blue;
if (mycolor == green) mycolor = red;
Enumerations are type compatible with numeric variables, so their constants are always assigned an integer numerical value internally. If it is not specified, the integer value equivalent to the first possible value is equivalent to
0and the following ones follow a +1 progression. Thus, in our data type
colors_tthat we have defined above,
blackwould be equivalent to
0,
bluewould be equivalent to
1,
greento
2, and so on.
We can explicitly specify an integer value for any of the constant values that our enumerated type can take. If the constant value that follows it is not given an integer value, it is automatically assumed the same value as the previous one plus one. For example:
1
2
3
enum months_t { january=1, february, march, april,
may, june, july, august,
september, october, november, december} y2k;
In this case, variable
y2kof enumerated type
months_tcan contain any of the 12 possible values that go from
januaryto
decemberand that are equivalent to values between
1and
12(not between
0and
11, since we have made
januaryequal to
1).
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4