C++ const 指针的 typedef 解释
首先是示例代码:
情况 1:
typedef char* CHARS;
typedef CHARS const CPTR; // constant pointer to chars
以文本方式替换 CHARS 变为:
typedef char* const CPTR; // still a constant pointer to chars
情况 2:
typedef char* CHARS;
typedef const CHARS CPTR; // constant pointer to chars
以文本方式替换 CHARS 变为:
typedef const char* CPTR; // pointer to constant chars
在情况 2 中,以文本方式替换 CHARS 后,typedef 的含义发生了变化。为什么会这样呢? C++ 如何解释这个定义?
Firstly, sample codes:
Case 1:
typedef char* CHARS;
typedef CHARS const CPTR; // constant pointer to chars
Textually replacing CHARS becomes:
typedef char* const CPTR; // still a constant pointer to chars
Case 2:
typedef char* CHARS;
typedef const CHARS CPTR; // constant pointer to chars
Textually replacing CHARS becomes:
typedef const char* CPTR; // pointer to constant chars
In case 2, after textually replacing CHARS, the meaning of the typedef changed. Why is this so? How does C++ interpret this definition?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基于文本替换来分析
typedef
行为是没有意义的。 Typedef 名称不是宏,它们不会被文本替换。正如您指出的那样,您自己
与 This 是同一件事,
出于同样的原因,为什么
与 Typedef-name 具有相同的含义
不定义新类型(仅是现有类型的别名),但它们在某种意义上是“原子”的任何限定符(如
const
)都适用于最顶层,即它们适用于隐藏在typedef名称后面的整个类型。一旦定义了 typedef-name,您就无法将限定符“注入”到其中,以便它可以修改该类型的任何更深层次。There's no point in analyzing
typedef
behavior on the basis of textual replacement. Typedef-names are not macros, they are not replaced textually.As you noted yourself
is the same thing as
This is so for the very same reason why
has the same meaning as
Typedef-name don't define new types (only aliases to existing ones), but they are "atomic" in a sense that any qualifiers (like
const
) apply at the very top level, i.e. they apply to the entire type hidden behind the typedef-name. Once you defined a typedef-name, you can't "inject" a qualifier into it so that it would modify any deeper levels of the type.Typedef 不是简单的文本替换。
意味着“CPTR 类型将是一个 const CHARS 类型。”但 CHARS 是一个指向字符的指针类型,因此这表示“CPTR 类型将是一个 const 指向字符的指针类型”。这与您进行简单替换时看到的不匹配。
换句话说,
是不相同
typedef 语法就像变量声明,只不过它不是将目标名称声明为变量,而是将其声明为可以使用的新类型名称声明没有 typedef 的变量的类型。
以下是确定 typedef 声明内容的简单过程:
删除
typedef
关键字。现在您将有一个变量声明。找出该变量的类型(某些编译器有一个
typeof()
运算符,它正是执行此操作并且非常有用)。将该类型称为 T。在本例中,是一个指向(非常量)char 的常量指针。替换
typedef
。您现在声明一个新类型 (CPTR
),它与 T 完全相同,是一个指向(非常量)char 的常量指针。Typedef is not a simple textual substitution.
Means "the CPTR type will be a const CHARS thing." But CHARS is a pointer-to-char type, so this says "the CPTR type will be a const pointer-to-char type." This does not match what you see when you do a simple substituion.
In other words,
is not the same as
The typedef syntax is like a variable declaration, except that instead of declaring the target name to be a variable, it declares it as a new type name which can be used to declare variables of the type that the variable would be without the typedef.
Here's a simple process for figuring out what a typedef is declaring:
Remove the
typedef
keyword. Now you will have a variable declaration.Figure out what type that variable is (some compilers have a
typeof()
operator which does exactly this and is very useful). Call that type T. In this case, a constant pointer to (non-constant) char.Replace the
typedef
. You are now declaring a new type (CPTR
) which is exactly the same type as T, a constant pointer to (non-constant) char.