その先にあるもの…

[C++]const_cast 본문

프로그래밍

[C++]const_cast

specialJ 2018. 12. 12. 17:30

형을 변환 시켜주는 것이 아니라

 const, 참조를 제거한다.


char str[] = "BitCoin";

const char* ptr = str;

cout << " before : " << str << endl;    (before : BitCoin)


//str[0] = 'b'; //error

char* remove_const = const_cast<char*>(ptr);


remove_const[0] = 'b';

cout << " after 0: " << str << endl;    (after 0 : bitCoin)


remove_const[0] = 'B';

cout << " after 1: " << str << endl;    (after 1 : BitCoin)




const int value = 10;

const int& ref = value;

int& ref2 = const_cast<int&>(ref);

ref2 = 20;

cout << "value(" << value << ") " << " ref(" << ref << ") " << " ref2(" << ref2 << ")" << endl;

Value(10)  ref(20)  ref2(20)


'프로그래밍' 카테고리의 다른 글

정규표현식 정리  (0) 2019.09.18
[C++] dynamic_cast  (0) 2018.12.12
[C++]LNK2019  (0) 2018.10.19
[SHELL SCRIPT] 쉘스크립트IF  (0) 2018.03.29
[SHELL SCRIPT] 쉘 스크립트 scp 예제  (0) 2018.03.29
Comments