컴퓨터공부/C & C++ & STL

Cfaq 11

achivenKakao 2009. 4. 15. 01:32
Q 11.11 다음과 같은 선언에서:
typedef char *charp;
const charp p;
왜 p가 가리키는 char가 const가 되지 않고, p 자체가 const가 되는 것
일까요?
Answer Typedef로 치환한 것은 순수하게 textual 치환이 아닙니다. (이 것은 typedef를
쓰는 한가지 장점이기도 합니다; 질문 1.13 참고) 다음과 같은 선언
에서:
const charp p;
const int i가 i를 const로 만드는 것과 같은 원리에서, p는 const가
됩니다. p에 대한 선언은, 포인터가 관련이 되어있는지 typedef 안까지 쫓
아가서 확인하지 않습니다.
Note 아래와 같은 선언이 있다고 가정하고 (질문 11.9 참고):
int * const const_pointer;
위에서 const_pointer는 typedef를 써서 다음과 같이 쓸 수 있습니다:
typedef int *int_pointer;
const int_pointer const_pointer;
이 때, const_pointer는 상수 int를 가리키는 포인터처럼 보이지만, 실제
로는 (상수가 아닌) int를 가리키는 const 포인터입니다. 또, 타입 specifier
와 타입 qualifier의 순서는 중요하지 않기 때문에 (순서가 바뀔 수 있기 때
문에), 다음과 같이 쓸 수도 있습니다:
int_pointer const const_pointer;