8.5 Explicit type conversion (casting) and static_cast

주홍영·2022년 3월 12일
0

Learncpp.com

목록 보기
81/199

https://www.learncpp.com/cpp-tutorial/explicit-type-conversion-casting-and-static-cast/

앞서 언급했던 명시적 type conversion의 방법인
static_cast를 사용하는 방법에 대해서 다루고 있다

int x = 1;
double y = (double)x;
위와 같은 방법은 c-style의 casting이다
As a result, C-style casts are at risk for being inadvertently misused and not producing the expected behavior, something which is easily avoidable by using the C++ casts instead
따라서 c-style의 casting은 지양한다

Static_cast

c++에서 캐스팅 operator는 static_cast다
사용법은 다음과 같다

static_cast<type>(target);

위의 static_cast는
narrowing conversion (<->promotion or widen) 즉, 데이터손실 발생하는 casting에 대해
프로그래머가 의도한 것이라는 것을 명시적으로 나타낼 수 있다

profile
청룡동거주민

0개의 댓글