上代码:
typedef char *pstring;
int main(){
char a ='a';
const pstring cstr = &a; // 顶层const 数据 指针常量
const pstring *sp;
*cstr = 'c';
cout << *cstr << endl; // c
// cout<< *sp << endl;
return 0;
}
解释:
今天看primer发现自己对类型别名理解有点偏差,总是想成宏定义,直接替换,看了这个链接终于搞懂了
英文原文是:
The base type in these declarations is const pstring. As usual, a const that appears in the base type modifies the given type. The type of pstring is “pointer to char”.So, const pstring is a constant pointer to char–not pointer to const char.
也就是说这里pstring 作为类型别名就是base type, const 作为修饰放在其左边右边是一回事。