gpt4 book ai didi

c++ - 在 C++ 中使用 const 的指针和符号位置

转载 作者:可可西里 更新时间:2023-11-01 17:35:31 24 4
gpt4 key购买 nike

<分区>

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

我一直想知道放置*& 的确切正确位置是什么。似乎 C++ 对放置这些标记的位置相当宽容。例如,我似乎将指针和符号放在关键字的左右两侧或两个关键字的中间,但令人困惑的是,有时它们似乎意味着同一件事,尤其是与 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);

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

编辑:

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

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

编辑:我想大致了解职位事宜。如果是,在什么情况下。

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