typedef, enum, struct, union P.J. Drongowski 19 October 2004 *********************************** typedef *********************************** * With typedef, you can declare and use new type names, e.g., typedef unsigned long uint32 ; * The new type name can be used like a built-in type name to declare variables, pointer variables, etc. * It's common practice to define a new type and a pointer to the new type in the same declaration: typedef long int Capacity, *PtrCapacity ; * It's good practice to capitalize the first letter of a type name *********************************** enum *********************************** * An enumeration is a way to associate a set of identifiers to numeric constants enum Color { RED, GREEN, BLUE, VIOLET, YELLOW } ; * An enumeration type is a good way to define a set of mutually exclusive options, values, etc. enum TokenType { IDENTIFIER, NUMBER, DELIMITER } ; * Enumerations can be anonymous * The enumeration name can be used to declare variables of that particular enumeration type *********************************** struct datatype (aggregation) *********************************** * The C++ struct datatype defines a collection of data items; It is an "aggregation" of data items (members) struct { int x, y ; int red, green, blue ; double intensity ; } ; * Hey, a struct sounds a lot like a class where all member functions and data members are public + Yep, you're right + Member functions are rarely defined for a struct * You refer to members of a struct using the familiar "." and "->" member selection operators + Use "." with a struct itself + Use "->" with a pointer to struct * struct datatypes are most useful when declared as a C/C++ datatype using typedef ***************** Example ***************** typedef struct _pixel { int x, y ; int red, green, blue ; double intensity ; struct _pixel *pixel ; } Pixel, *PtrPixel ; int main(int argc, char *argv) { Pixel u, v, w ; u.x = 10 ; u.y = 15 ; u.red = 255 ; u.green = 0 ; u.blue = 0 ; u.intensity = 1.0 ; u.pixel = &v ; u.pixel->x = 20 ; u.pixel->y = 30 ; } ******************** union ******************** * A union appears like a struct with multiple data members * However, all members occupy the same physical memory locations (they overlay one another) * A union occupies as much space as required by the largest member * A union is used where the same physical member locations must be (re)interpreted or (re)used in different ways * Example: int iv ; double dv ; union poly_value { long long int int_value ; double double_value ; } numeric_value ; ... numeric_value.int_value = 35 ; iv = numeric_value.int_value ; ... numeric_value.double_value = 97.0 ; dv = numeric_value.double_value ; * A union is usually used as part of a struct that includes a "tag" which indicates the current type of data stored in the union enum PolyType { POLY_UNDEFINED, POLY_INT, POLY_DOUBLE } ; int iv ; double dv ; struct poly_value { PolyType tag ; union poly_union { long long int int_value ; double double_value ; } value ; } numeric_value ; ... switch( numeric_value.tag ) { case POLY_INT: numeric_value.value.int_value = 35 ; iv = numeric_value.value.int_value ; break ; case POLY_DOUBLE: numeric_value.value.double_value = 97.0 ; dv = numeric_value.value.double_value ; break ; } * A union like this could be used to build a stack that handles either integer or floating point values