포인터 상수화
constant
실습 예제
ch04ex5_const.cpp
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int x = 30;
int y = 10;
const int* px = &x; // 포인터 역참조 불가
x = 50; // x값 변경 가능
// *px = 50; // 메모리안의 값 변경(역참조) 불가 오류 발생
px = &y; // 참조 주소는 변경 가능
cout << "x: " << x << ", *px: " << *px << endl;
/* ------------------------------------------------------ */
int* const py = &y;
*py = 99;
// py = &x; // 참조 주소 변경 불가
cout << "y: " << x << ", *yx: " << *py << endl;
const int* const pz = &x; // 역참조, 참조 모두 변경 불가
cout << *pz << endl;
return 0;
}Last updated