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

发布于 2024-09-11 08:41:41 字数 355 浏览 15 评论 0原文

在 C++ 中,以下之间有什么区别:

 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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咽泪装欢 2024-09-18 08:41:41

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

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

类似地,应用于正在声明的变量的类型修饰符[]会将变量的类型修改为数组,而<应用于数组类型的对象的strong>二元中缀运算符[]将访问该数组的子对象之一。


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

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

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

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

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

事实上,C 和 C++ 并不关心类型修饰符周围的空格,这导致了在放置它们时出现不同的阵营并没有真正让事情变得更容易。
有些人将类型修饰符放在靠近类型的位置。他们认为它修改了类型,因此应该放在那里:

int* ptr;

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

int* a, b;

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

int *ptr;
int *a, *b;

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


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

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

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; 

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;

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

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;

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;

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.


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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文