Instagram
Recent Posts
Recent Comments
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

기록중

[C++] explicit 키워드 본문

Study/C\C++

[C++] explicit 키워드

코튼아 2016. 2. 24. 17:40
class Int32
{
public:
	int val;
	// 변환 생성자: 어떠한 키워드를 사용해서 변환 생성자를 만드는 것이 아니라, 인자가 1개인
	// 생성자는 모두 변환 생성자로 사용될 수 있다. --> 버그임. 이를 해결하기 위해 "explicit"가 나옴.
	explicit Int32(int v) : val(v) {}
};

void print_int32(Int32 i32) //Int32 i32(n)
{
	cout << i32.val << endl;
}
int main()
{
	Int32 i32(1); // -> Int32 i32 = Int32(1); -> Int32 i32(Int32(1));
	print_int32(i32);

	int n = 1;
	print_int32(n); // explicit 키워드 사용시 ERROR
}


Comments