C ++中带有const的指针和与号位置

2022-01-23 00:00:00 pointers constants c++

可能重复:
声明指针;类型和名称之间的空格是在左边还是右边的星号?

我一直想知道放置 *& 的确切位置是什么.似乎 C++ 对放置这些标记的位置非常宽容.例如,我似乎将指针和 & 号放在关键字的左右两侧或两个关键字的中间,但令人困惑的是,有时它们似乎意味着相同的东西,尤其是与 const

I've always been wondering what's the exact correct position to put * and &. it seems that C++ is pretty tolerant about where to put these marks. For example I've seem pointer and ampersand been put on both left and right of a keyword or in the middle of two keywords, but confusingly sometimes they seems to mean the same thing, especially when used together with const

void f1(structure_type const& parameter)
void f2(structure_type const &parameter)

void f2(structure_type  const *sptr);
void f2(structure_type  const* sptr);
void f2(structure_type  const * sptr);

示例并不详尽.在声明或传递给函数时,我到处都能看到它们.他们甚至是同一个意思吗?但我也看到放置 * 的情况会影响将哪个对象称为指针(可能是 * 位于两个关键字之间的情况).

The examples are not exhaustive. I see them everywhere while being declared or been passed to a function. Do they even mean the same thing? But I also see cases while putting * will affect which object being referred as a pointer (likely the case where * is in between two keywords).

int const *Constant
int const * Constant // this above two seem the same to me, both as pointer to a constant value
int const* Constant //  EDIT: this one seems the same as above. instead of a constant pointer

const int * Constant // this is also a pointer to a constant value, but the word order changed while the pointer position stays the same, rather confusing.

int* const Constant
int * const Constant // instead, these two are constant pointers

所以我得出结论:

T const* p; // pointer to const T
const T* p  // seems same from above
T* const p; // const pointer to T

不过,这让我很困惑.编译器不关心它们所需的位置和间距吗?

Still, this confused the hell out of me. Doesn't the compiler care about position and the spacing required for them?

我想大致了解职位的重要性.如果是,在什么情况下.

I want to know in general of the position matters. If yes in what cases.

推荐答案

空格的重要性在于它阻止令牌一起运行和(例如)创建单个令牌,所以(例如)intx 明显不同于 intx.

White space matters only to the degree that it keeps tokens from running together and (for example) creating a single token, so (for example) int x is obviously different from intx.

当您处理类似以下内容时:int const*x;* 的任一大小上的空格对编译器完全没有影响.

When you're dealing with something like: int const*x;, whitespace on either size of the * makes absolutely no difference to the compiler at all.

pointer to const intconst pointer to int 之间的区别取决于 const 在 * 的哪一侧.

The difference between a pointer to const int and const pointer to int depends on which side of the * the const is on.

int const *x;    // pointer to const int
int *const x;    // const pointer to int

主要区别在于当/如果您在同一声明中定义/声明多个对象时的可读性.

The primary difference is readability when/if you define/declare multiple objects in the same declaration.

int* x, y;
int *x, y;

首先,有人可能认为 x 和 y 是指向 int 的指针――但实际上,x 是指向 int 的指针,而 y 是 int.在某些人看来,第二个更准确地反映了这一事实.

In the first, somebody might think that x and y are pointers to int -- but in fact, x is a pointer to an int, and y is an int. To some people's eyes, the second reflects that fact more accurately.

防止任何误解的一种方法是一次只定义一个对象:

One way to prevent any misinterpretation is to only ever define one object at a time:

int *x;
int y;

对于其中任何一个,如果您完全忽略空格(除了告诉您一个令牌在哪里结束而另一个开始,所以您知道const int"是两个令牌)并从右到左阅读,正确的解释是相当容易的,阅读* 作为指针".例如:int volatile * const x; 读作x 是指向 volatile int 的 const 指针".

For any of these, correct interpretation is fairly easy if you ignore whitespace entirely (except to tell you where one toke ends and another starts, so you know "const int" is two tokens) and read from right to left, reading * as "pointer to". For example: int volatile * const x; is read as "x is a const pointer to a volatile int".

相关文章