https://www.youtube.com/watch?v=rz96pwnoY3k&list=PLgqG2uj21HgkcfVtlr5rPekQl5VWJEnIB&index=11
서로 다른 데이터형을 한번에 한가지만 보관할 수 있다. 데이터가 들어올때마다 이전데이터가 소실되어 다음과 같은 결과가 나온다.
#include <iostream>
using namespace std;
int main(){
union MYUnion
{
int intVal;
long longVal;
float floatVal;
};
MYUnion test;
test.intVal = 3;
cout << test.intVal << endl;
test.longVal = 33;
cout << test.intVal << endl;
cout << test.longVal << endl;
test.floatVal = 3.3;
cout << test.intVal << endl;
cout << test.longVal << endl;
cout << test.floatVal << endl;
}
열거체는 기호 상수를 만드는 것에 대한 또 다른 방법이다.
열거체 만드는 방법
오렌지를 출력해보면 1이라는 숫자가 나오는것을 볼 수 있다.
#include <iostream>
using namespace std;
int main(){
enum spectrum {
red, orange, yellow, green, blue, violet, indigo
};
spectrum a = orange;
cout << a << endl;
}
만약 열거체의 숫자를 직접지정하게 된다면 그 이후의 값은 차례로 숫자가 붙게 된다. 예를 들어서 orange를 10으로 지정하면 yellow부터 11,12,13... 이렇게 붙는다.
#include <iostream>
using namespace std;
int main(){
enum spectrum {
red, orange = 10, yellow, green, blue, violet, indigo
};
spectrum a = orange;
cout << a << endl;
}