* 和 & 是什么意思?什么时候应用于变量名?

2021-12-13 00:00:00 reference pointers c++ c++-faq

在C++中,有什么区别:

In C++, what is the difference between:

 void func(MyType&); // declaration

 //...
 MyType * ptr;

 func(*ptr); // compiler doesnt give error

 func(ptr); // compiler gives error i thought & represents memory address so 
            // this statement should correct as ptr is only a pointer
            // or address of some real var.

推荐答案

一元前缀运算符 &,应用于对象时, 产生对象的地址:&obj.
类型修饰符&,当应用于即将声明的变量时,会将变量的类型修改为引用类型:int&.

The unary prefix operator &, when applied to an object, yields the address of the object: &obj.
The type modifier &, when applied to a variable about to be declared, will modify the variable's type to be a reference type: int&.

同样适用于*:当作为一元前缀运算符应用于指针时,它将取消引用 指针,产生引用的对象:*ptr.
当用作将要声明的变量的类型修饰符时,*会将类型修改为指针:int*.

The same applies to *: When applied as a unary prefix operator to a pointer, it will dereference the pointer, yielding the object referred to: *ptr.
When used as a type modifier to a variable about to be declared, * will modify the type to be a pointer: int*.

以类似的方式,应用于正在声明的变量的类型修饰符[]会将变量的类型修改为数组,而应用于数组类型对象的 二元中缀运算符 [] 将访问数组的子对象之一.

In a similar way, the type modifier [] applied to a variable that's being declared will modify the variable's type to an array, while the binary infix operator [] applied to an object of array type will access one of the array's sub-objects.

类型修饰符应用于声明的变量,而不是声明它们的类型,这没有帮助.例如,这个

It's not helpful that type modifiers apply to the variable that is declared, not to the type they are declared with. For example, this

int *p, **pp, i, a[10], &r = i; 

定义了一个int指针,一个指向int的指针,一个普通的int,一个10个int的数组int 引用.(后者立即初始化,因为您不能有未初始化的引用.)请注意,类型修饰符在语法上属于它们正在修改的类型的声明变量,而不属于声明变量的类型.尽管如此,类型修饰符(*&)会修改变量的 type.
但是,在以下情况下,pia 假定为已经声明的变量

defines an int pointer, a pointer to a pointer to an int, a vanilla int, an array of 10 int, and an int reference. (The latter is immediately initialized, because you cannot have an uninitialized reference.) Note that the type modifiers syntactically belong to the declared variable whose type they are modifying, not to the declared variable's type. Nevertheless, type modifiers (* and &) modify the type of the variable.
In the following case, however, with p, i, and a presumed to be variables that have already been declared

*pp = &i;
a[0] = i;

*& 是取消引用 pp 并产生 i 的地址的一元前缀运算符,而 [] 产生数组 a 中的第一个 int 对象.

* and & are unary prefix operators dereferencing pp and yielding the address of i, while [] yields the first int object in the array a.

事实上,C 和 C++ 不关心类型修饰符周围的空格,这导致了不同的阵营放置它们并没有真正让事情变得更容易.
有些人将类型修饰符放在靠近类型的位置.他们争辩说它修改了类型,所以它应该去那里:

The fact that C and C++ don't care about the whitespaces around type modifiers and that this led to different camps when it comes to place them doesn't really make things easier.
Some people place the type modifiers close to the type. They argue that it modifies the type and so it should go there:

int* ptr;

缺点是在声明多个对象时会变得混乱.这个

The disadvantage is that this becomes confusing when declaring several objects. This

int* a, b;

a定义为指向int的指针,而b定义为int.这就是为什么有些人喜欢写作

defines a to be a pointer to int, but b to be an int. Which is why some people prefer to write

int *ptr;
int *a, *b;

我建议永远不要在同一个语句中声明多个对象.使代码更易于阅读的 IMO.此外,它让您可以自由选择任一约定.

I suggest to simply never declare multiple objects in the same statement. IMO that makes code easier to read. Also, it leaves you free to pick either convention.

更复杂的是,除了类型修饰符和一元前缀运算符&*,还有二元中缀运算符 &*,意思是按位与"和乘法".雪上加霜的是,在 C++ 中,您可以重载这些运算符的一元前缀和二元中缀变体(以及二元中缀 [])用于用户定义的类型,并对其语义完全自由.

To complicate things even further, besides the type modifiers and the unary prefix operators & and *, there are also the binary infix operators & and *, meaning "bitwise AND" and "multiplication". And to add insult to injury, in C++ you can overload both the unary prefix and the binary infix variants of these operators (and the binary infix []) for user-defined types and be completely free as to their semantics.

相关文章