예시
#include <iostream>
using namespace std;
int main() {
union Myunion {
int intVal;
long longVal;
float floatVal;
};
Myunion test;
test.intVal = 3;
cout << test.intVal << endl; //3
test.longVal = 33;
cout << test.intVal << endl; //33
cout << test.longVal << endl; //33
test.floatVal = 3.3;
cout << test.intVal << endl; //1079194419
cout << test.longVal << endl; //1079194419
cout << test.floatVal << endl; //3.3
}
장점 - 메모리절약
#include <iostream>
using namespace std;
int main() {
enum spectrum { red, orange, yellow, green, blue, violet, indigo, ultraviolet}; //0부터 1씩 더해지는 정수가 저장된다.
//만약 명시적으로 값을 다르게 하고싶다면 red = 0, orange = 2 이런식으로 가능
// 만약 orange 다음 값을 지정해주지 않는다면 +1씩 더해진다.
// 1. spectrum을 새로운 데이터형 이름으로 만든다.
// 2. red, orange, yellow... 0부터 7까지 정수 값을 각각 나타내는 기호 상수로 만든다.
spectrum a = orange;
cout << a << endl; // 1
//기호 상수로서 관리하기 때문에 사용할 수 없다.
//열거자들끼리의 산술 연산은 지원하지 않는다
//ex) orange + yellow
int b = blue + 3; // 3 + 4 가 된다.
cout << b << endl;
}