类运算符之间的区别
我正在阅读一点 C++,想知道是否有人可以向我解释一些类运算符的一些差异。有问题的运算符是:
* & 。 ->
我理解其中一些,但是,我看不出一些之间的区别。例如:
*x
&x
x.y
(*x).y
x -> y
有人可以举个例子并解释一下它们有何不同吗?
I am reading into C++ a little bit and was wondering if someone could explain some differences of some of the class operators to me. The operators in question are:
* & . ->
I understand some of them, however, I fail to see the difference between some. For example:
*x
&x
x.y
(*x).y
x -> y
Can someone use an example and explain a bit how they would differ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
采用我的示例中给出的这个小结构:
*x
取消引用指针x
。返回值是对x
指向的对象的引用:&x
采用x
的地址。因此,结果是一个指向 x 的指针。xy
访问对象x
中名为y
的成员。(*x).y
是第一个和第三个的组合:指针被取消引用,并且.
运算符用于访问它的对象的成员指向.x->y
是(除非被邪恶的人覆盖)(*x).y
的快捷方式。Take this small structure as given for my samples:
*x
dereferences the pointerx
. The return value is a reference to the object pointed to byx
:&x
takes the address ofx
. The result is therefore a pointer tox
.x.y
accesses a member namedy
in the objectx
.(*x).y
is a combination of the first and the third: the pointer is dereferenced, and the.
-operator is used to access a member of the object it points to.x->y
is (unless overridden by evil people) a shortcut for(*x).y
.*x
是取消引用运算符,如果您有memoryAddress
,则*memoryAddress
是相应的对象。相反,
&x
是引用运算符,如果你有一个object
,那么&object
提供内存地址。对于访问变量,
x->y
基本上是(*x).y
的快捷方式,即取消引用然后访问成员。*x
is the dereference operator, if you have amemoryAddress
, then*memoryAddress
is the respective object.The converse,
&x
is the reference operator, if you have anobject
, then&object
provides the memory address.For access variables,
x->y
is basically a shortcut of(*x).y
, i.e. dereferencing and then accessing a member.继续阅读!我将简要介绍几个,但在您真正理解一些基本概念之前,它可能仍然没有意义。
*x 是解引用运算符。这意味着它将“x”视为内存地址,并查找该内存位置中的内容并返回该值。因此,如果 x = 100,*x 会查找内存地址 100 中的内容,并返回该内存位置中的值。
&x 返回变量 x 存储的地址。再想象一下,如果 x = 100,但值 100 存储在地址 50 处。&x 将返回 50,x 将返回 100,*x 返回存储的值在内存地址 100 中。
其他的需要更多的解释,但也需要对类和结构有更多的理解。我建议您继续阅读并做更多示例,这可能比现在冗长的解释更有帮助。
Keep reading! I'll give a nutshell of a couple but it might still not make sense until you really understand some of the underlying concepts.
*x is a dereferencing operator. It means that it treats 'x' as an address to memory, and it looks up what is in that memory location and returns that value. So if x = 100, *x looks up what's in memory address 100, and returns the value in that memory location.
&x returns the address that the variable x is stored in. Imagine again if x = 100, but the value 100 is stored at address 50. &x would return 50, x would return 100, and *x returns the value stored in memory address 100.
The others require a bit more explanation, but also a bit more understanding of classes and structures. I suggest you continue reading and doing more examples, as will probably help more than a long-winded explanation now.
*x
是取消引用
运算符。也就是说,它是&
运算符的逆操作 - 它获取指针指向的内存的值。&
是一个返回变量内存地址的运算符。xy
表示访问x
的成员yx -> y
是(*x).y
的同义词*x
is thedereference
operator. That is, its the inverse of the&
operator - it gets the value of the memory pointed to by the pointer.&
is an operator that returns the memory address of the variable.x.y
means to access member y ofx
x -> y
is a synonym for(*x).y
&
获取对象的内存地址*
获取内存地址指向的对象
的成员->
从指向对象的指针访问成员&
gets the memory address of an object*
gets the object pointed to by a memory address.
accesses a member of an object->
accesses a member from a pointer to an object