C++: designated initializers
개요
- 구조체, 공용체 초기화 시 멤버 이름을 지정하여 초기화 가능
- 부분 초기화 가능
- 정의 선언 순서와 같아야 함
예제
- 코드
#include <iostream>
using namespace std;
struct Test {
int x;
int y;
int z;
};
int main() {
Test t{.x = 1, .z = 3};
cout << t.x << endl;
cout << t.y << endl;
cout << t.z << endl;
// Test t2{.y = 2, .x = 1, .z = 3};
return 0;
}
- 실행 결과
1
0
3