독개

#따배씨 4.4 auto키워드와 자료형 추론

by #독개#

//int a = 123; 이안에는 123이 정수인것만 봐도 정수형인것을 알수있다
//auto a = 123; 해주면 알아서 a를 정수형으로 선언해준다
//함수의 return 값의 자료형도 알아서 잡아준다
//함수의 자료형도 auto로 알아서잡아준다
//Parameter는 안된다 이건 templete라는걸 쓰게되는데 이건 강의 후반부에 배운다

//trailing return type이라고
//auto add(int x, int y) -> int; 이렇게 할수도있다
//int add하면되지 구지? 라고 할수있지만 그냥 깔끔하게 볼수있어서 뭐 이런방식도있다정도

#include <iostream>

int doSomething()
{
	return 3;
}

auto add(int x, int y) //알아서 실수형으로 잡아준다
{
	return x + (double)y;
}

int main()
{
	using namespace std;

	auto a = 1;
	auto c = 1 + 2.0;
	auto d = 'a';
	auto e = doSomething(); //알아서 e의 자료형을 int로 잡아줌
	cout << "Hello" << endl;

}

 

 

 

 


🐱‍👓독하게 개발

블로그의 정보

독한 개발자

#독개#

활동하기