8.6 Typedefs and type aliases

주홍영·2022년 3월 12일
0

Learncpp.com

목록 보기
82/199

https://www.learncpp.com/cpp-tutorial/typedefs-and-type-aliases/

Type aliases

alias (별칭, 별명)
c++에서는 using keyword를 이용해 type alias를 설정할 수 있다

using distance_type = double; // Also ok, more about this in a later chapter
using distance = double; // Also ok, but could be confused for- and collide with variable names

Typedef

typedef또한 별칭을 지어주는 위의 효과와 똑같은 역할을 하는 키워드 이다
예시는 다음과 같다

// The following aliases are identical
typedef long miles_t;
using miles_t = long;

주의

typedef의 경우 type이 먼저 alias가 나중에 온다는 것을 명심하자!!

Using type aliases to make complex types simple

이러한 type alias를 생성하는 기능을 이용해
복잡한 type을 간단하게 사용할 수 있다
예를 들어

using pairlist_t = std::vector<std::pair<std::string, int>>;

위와 같이 말이다

Using type aliases for legibility

legibility : 읽기 쉬움
alias를 이용해서 우리는 가독성을 높일 수도 있다

using testScore_t = int;
testScore_t gradeTest();

Conclusion

이러한 이점이 있지만 혼동이 생길 수 있으므로
정말 사용했을 때 이점이 뚜렷한 경우 사용하길 바란다

profile
청룡동거주민

0개의 댓글