typedef
keyword:
typedef existing_type new_type_name ;
existing_type
is any type, either fundamental or compound, and new_type_name
is an identifier with the new name given to the type.
|
|
C
, WORD
, pChar
, and field
as char
, unsigned int
, char*
and char[50]
, respectively. Once these aliases are defined, they can be used in any declaration just like any other valid type:
|
|
|
|
|
|
typedef
and aliases defined with using
are semantically equivalent. The only difference being that typedef
has certain limitations in the realm of templates that using
has not. Therefore, using
is more generic, although typedef
has a longer history and is probably more common in existing code.typedef
nor using
create new distinct data types. They only create synonyms of existing types. That means that the type of myword
above, declared with type WORD
, can as well be considered of type unsigned int
; it does not really matter, since both are actually referring to the same type.int
to refer to a particular kind of parameter instead of using int
directly, it allows for the type to be easily replaced by long
(or some other type) in a later version, without having to change every instance where it is used.union type_name { member_type1 member_name1; member_type2 member_name2; member_type3 member_name3; . . } object_names; |
type_name
, in which all its member elements occupy the same physical space in memory. The size of this type is the one of the largest member element. For example:
|
|
mytypes
) with three members:
|
|
|
|
int
type with a size of 4 bytes, and a short
type of 2 bytes, the union defined above allows the access to the same group of 4 bytes: mix.l
, mix.s
and mix.c
, and which we can use according to how we want to access these bytes: as if they were a single value of type int
, or as if they were two values of type short
, or as an array of char
elements, respectively. The example mixes types, arrays, and structures in the union to demonstrate different ways to access the data. For a little-endian system, this union could be represented as:structure with regular union | structure with anonymous union |
---|---|
|
|
price
), while in the second it has not. This affects the way to access members dollars
and yen
of an object of this type. For an object of the first type (with a regular union), it would be:
|
|
|
|
dollars
and yen
actually share the same memory location, so they cannot be used to store two different values simultaneously. The price
can be set in dollars
or in yen
, but not in both simultaneously.enum type_name { value1, value2, value3, . . } object_names; |
type_name
, which can take any of value1
, value2
, value3
, ... as value. Objects (variables) of this type can directly be instantiated as object_names
.colors_t
could be defined to store colors with the following declaration:
|
|
color_t
may take are the enumerators listed within braces. For example, once the colors_t
enumerated type is declared, the following expressions will be valid:
|
|
enum
are implicitly convertible to an integer type. In fact, the elements of such an enum
are always assigned an integer numerical equivalent internally, to which they can be implicitly converted to. If it is not specified otherwise, the integer value equivalent to the first possible value is 0
, the equivalent to the second is 1
, to the third is 2
, and so on... Therefore, in the data type colors_t
defined above, black
would be equivalent to 0
, blue
would be equivalent to 1
, green
to 2
, and so on...
|
|
y2k
of the enumerated type months_t
can contain any of the 12 possible values that go from january
to december
and that are equivalent to the values between 1
and 12
(not between 0
and 11
, since january
has been made equal to 1
).enum
types that are neither implicitly convertible to int
and that neither have enumerator values of type int
, but of the enum
type itself, thus preserving type safety. They are declared with enum class
(or enum struct
) instead of just enum
:
|
|
enum class
type needs to be scoped into its type (this is actually also possible with enum
types, but it is only optional). For example:
|
|
enum class
also have more control over their underlying type; it may be any integral data type, such as char
, short
or unsigned int
, which essentially serves to determine the size of the type. This is specified by a colon and the underlying type following the enumerated type. For example:
|
|
Eyecolor
is a distinct type with the same size of a char
(1 byte).Index |