如何在包含 typedef 时用从右到左的规则解释变量声明
How to interpret variable declarations with right to left rule while typedef is included?
当
涉及typedef时,使用从右到左的规则来解释变量声明时遇到问题。
在C++入门第 5 版书中,我看到了以下代码:
typedef char *pstring;
const pstring cstr = 0; // cstr is a constant pointer to char
const pstring *ps; // ps is a pointer to a constant pointer to char
如果我用 char * 替换字符串,那么它就是这样:
const char *cstr
所以我希望 cstr 是指向常量字符的指针。但书中的注释指出指针本身是恒定的。我的问题是我的思维方式出了什么问题。
> typedef
不是宏。您不只是用文本替换它。
将其读作cstr
是一个"常量pstring
",这是一个"常量(指向字符的指针("。将其与const char*
进行比较,后者是"指向常量char
的指针"。
如果要替换 typedef,它将如下所示:
char* const cstr = 0;
char* const* ps;
可以在这里阅读:
如果使用 const 类型限定符(通过 使用typedef(,数组类型不是常量限定的,但其 元素类型为
因为 pstring 是 typedef 到 char *
,所以const pstring cstr
是char * const cstr
,而不是const char * cstr
。
相关文章:
- 请解释"函数1(p1,p2,p3);"的输出
- 此代码是否违反一个定义规则
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 生成文件不对文件使用隐式规则
- 被解释为低级别const的const对象的地址
- 计算每个节点的树高,帮助我解释这个代码解决方案
- MSVC将仅移动结构参数解释为指针
- 变量可能尚未初始化[MIRA 2012规则9.1,强制性]
- 静态结构和一个定义规则
- 内联程序集printf将整数解释为地址
- 有人能解释一下为什么下界是这样工作的吗C++的
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 我是c ++的新手,你能解释一下在这种情况下的指针吗
- 有人能为我解释一下C++代码吗
- 如何在不违反类型别名规则的情况下解释消息负载?
- 如何在包含 typedef 时用从右到左的规则解释变量声明
- 有人能解释一下最多一次不变和存在、所有权和守恒规则是什么吗?
- 该"Named Constructor Idiom"似乎与静态方法无法访问非静态成员函数的规则相矛盾。有什么解释吗?
- 如何解释规则[命名空间].c++标准中的Udir]p2
- 有人能给我解释一下模板解析顺序规则吗?