Last active
September 7, 2020 06:05
-
-
Save kyagrd/68dd107c5b0a836b81e34245ab0838bf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
int main() | |
{ | |
using namespace std; | |
int64_t x = 0; | |
// C/C++에서 실수하기 쉬운 부분 단점 | |
x = 3.14592; // 자동으로 형변환되어서 3이 되어버림 | |
cout <<x <<endl; | |
// C에서는 아무 타입이나 마구마구 변환이 된다 | |
// C스타일의 강제 형변환 연산자 | |
x = (int64_t)"asdfsadf"; // 이거는 뭐 이해가 된다고 치자 문자열을 char 포인터 주소로 이해 | |
// 강제 형변환이 옛날 C에서는 되던 것들이 최신 C/C++ 표준에서는 에러로 처리되는 것들도 | |
// 방법은 | |
// (1) 컴파일러 옵션을 덜 최근 표준 기준으로 해달라고 옵션을 주기 | |
// ==> 별로 좋은 방법이 아니다 (컴파일러마다 다름) | |
// (2) C++에서 권장하는 방법은 새로운 강제 형변환 연산자 (여러가지 종류 3..개인가) | |
// static_cast 로 에러가 안나면 것들은 상당히 안전한 형변환 | |
// x = static_cast<int64_t>( "abcdef" ); // 가장 엄격하게 검사해서 error를 많이 내는 | |
// C스타일 형변환처럼 동작하는 C++의 강제 형변환 연산자 | |
x = reinterpret_cast<int64_t>( "asdfasdf" ); | |
cout <<x <<endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment