개발/C++

C++ 캐스트 연산자

알렉스키드 2023. 9. 15. 16:07
#include <iostream>

using namespace std;

int main()
{
	int x = 2;
	double y = 4.4;
	
	int a = static_cast<int>(y/x);
	int b = (int) y / x;
	double c =  y / x;
	
	cout << a << endl;	
	cout << b << endl;	
	cout << c << endl;		
		
	return 0;
}

 

2
2
2.2

 

 

static_cast<> 가장 기본적인 캐스트 연산 방법
dinamic_cast<> 객체지향의 다형성을 이용하여 모호한 타앱 캐스팅 오류 막아줌
const_cast<> 자료형이 갖고 있는 상수 속성을 제거
reinterpret_cast<> 어떠한 포인터 타입끼리도 변환할 수 있게 도움