C++ const 指针的 typedef 解释

发布于 2024-08-21 06:25:29 字数 553 浏览 6 评论 0原文

首先是示例代码:

情况 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜中书 2024-08-28 06:25:29

基于文本替换来分析 typedef 行为是没有意义的。 Typedef 名称不是宏,它们不会被文本替换。

正如您指出的那样,您自己

typedef CHARS const CPTR;

与 This 是同一件事,

typedef const CHARS CPTR;

出于同样的原因,为什么

typedef const int CI;

与 Typedef-name 具有相同的含义

typedef int const CI;

不定义新类型(仅是现有类型的别名),但它们在某种意义上是“原子”的任何限定符(如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

typedef CHARS const CPTR;

is the same thing as

typedef const CHARS CPTR;

This is so for the very same reason why

typedef const int CI;

has the same meaning as

typedef int const CI;

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.

暮凉 2024-08-28 06:25:29

Typedef 不是简单的文本替换。

typedef const CHARS CPTR;

意味着“CPTR 类型将是一个 const CHARS 类型。”但 CHARS 是一个指向字符的指针类型,因此这表示“CPTR 类型将是一个 const 指向字符的指针类型”。这与您进行简单替换时看到的不匹配。

换句话说,

typedef char * CHARS;

相同

#define CHARS char *

typedef 语法就像变量声明,只不过它不是将目标名称声明为变量,而是将其声明为可以使用的新类型名称声明没有 typedef 的变量的类型。

以下是确定 typedef 声明内容的简单过程:

  1. 删除 typedef 关键字。现在您将有一个变量声明。

    const CHARS CPTR;
    
  2. 找出该变量的类型(某些编译器有一个 typeof() 运算符,它正是执行此操作并且非常有用)。将该类型称为 T。在本例中,是一个指向(非常量)char 的常量指针。

  3. 替换typedef。您现在声明一个新类型 (CPTR),它与 ​​T 完全相同,是一个指向(非常量)char 的常量指针。

Typedef is not a simple textual substitution.

typedef const CHARS CPTR;

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,

typedef char * CHARS;

is not the same as

#define CHARS char *

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:

  1. Remove the typedef keyword. Now you will have a variable declaration.

    const CHARS CPTR;
    
  2. 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.

  3. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文