Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CPlusPlusNotesForProfessionals.pdf
Скачиваний:
58
Добавлен:
20.05.2023
Размер:
5.11 Mб
Скачать

Chapter 111: Unions

Section 111.1: Undefined Behavior

union U { int a; short b; float c;

}; U u;

u.a = 10;

if (u.b == 10) {

//this is undefined behavior since 'a' was the last member to be

//written to. A lot of compilers will allow this and might issue a

//warning, but the result will be "as expected"; this is a compiler

//extension and cannot be guaranteed across compilers (i.e. this is

//not compliant/portable code).

}

Section 111.2: Basic Union Features

Unions are a specialized struct within which all members occupy overlapping memory.

union U { int a; short b; float c;

}; U u;

//Address of a and b will be equal (void*)&u.a == (void*)&u.b; (void*)&u.a == (void*)&u.c;

//Assigning to any union member changes the shared memory of all members u.c = 4.f;

u.a = 5; u.c != 4.f;

Section 111.3: Typical Use

Unions are useful for minimizing memory usage for exclusive data, such as when implementing mixed data types.

struct AnyType { enum {

IS_INT,

IS_FLOAT } type;

union Data { int as_int;

float as_float; } value;

AnyType(int i) : type(IS_INT) { value.as_int = i; }

AnyType(float f) : type(IS_FLOAT) { value.as_float = f; }

GoalKicker.com – C++ Notes for Professionals

561

int get_int() const { if(type == IS_INT)

return value.as_int;

else

return (int)value.as_float;

}

float get_float() const { if(type == IS_FLOAT)

return value.as_float;

else

return (float)value.as_int;

}

};

GoalKicker.com – C++ Notes for Professionals

562