C ++中const的指针和与号位置 [英] pointer and ampersand position with const in C++

查看:115
本文介绍了C ++中const的指针和与号位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Possible Duplicate:
Declaring pointers; asterisk on the left or right of the space between the type and name?

我一直想知道放置*&的确切正确位置是什么.似乎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

所以我得出了这样的结论:

So I concluded this:

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.

推荐答案

空白仅在一定程度上可以使令牌无法一起运行并(例如)创建单个令牌,因此(例如)int xintx明显不同.

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

这篇关于C ++中const的指针和与号位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆