gpt4 book ai didi

c++ - C++ 引用中 & 符号在语义上的准确位置是什么

转载 作者:可可西里 更新时间:2023-11-01 15:05:30 24 4
gpt4 key购买 nike

众所周知,语义上准确的指针声明方式是

int *x;

代替

int* x;

这是因为 C 将 *x 视为 int,而不是将 x 视为 int 指针。

这很容易证明

int* a, b;

其中a是一个int指针,而b是一个int。

Stack Overflow 上至少有 5 个重复的问题讨论了这个问题以供引用。但是引用文献呢?

最佳答案

Bjarne Stroustrup 说:

A typical C programmer writes int *p; and explains it *p is what is the int emphasizing syntax, and may point to the C (and C++) declaration grammar to argue for the correctness of the style. Indeed, the * binds to the name p in the grammar.

A typical C++ programmer writes int* p; and explains it p is a pointer to an int emphasizing type. Indeed the type of p is int*. I clearly prefer that emphasis and see it as important for using the more advanced parts of C++ well.

当声明一个指针变量或参数时,您可以将星号(或&符号)放在类型或变量名称的旁边。

最重要的是在单个文件中始终如一地执行此操作。

// These are fine, space preceding.
char *c;
const int &i;

// These are fine, space following.
char* c;
const int& i;

关于c++ - C++ 引用中 & 符号在语义上的准确位置是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22766907/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com