类运算符之间的区别

发布于 2024-10-31 11:50:01 字数 213 浏览 4 评论 0原文

我正在阅读一点 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 技术交流群。

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

发布评论

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

评论(5

青朷 2024-11-07 11:50:01

采用我的示例中给出的这个小结构:

struct MyStruct {
   int y;
};

*x 取消引用指针x。返回值是对x指向的对象的引用:

MyStruct* x = ...;
MyStruct xval = *x;
// or
MyStruct& xref = *x;
// read up on references to understand the latter

&x采用x的地址。因此,结果是一个指向 x 的指针。

MyStruct x;
MyStruct* pointer_to_x = &x;

xy 访问对象 x 中名为 y 的成员。

MyStruct x;
x.y = 5;
int a = x.y + 2;
// a==7

(*x).y 是第一个和第三个的组合:指针被取消引用,并且 . 运算符用于访问它的对象的成员指向.

a = (*pointer_to_x).y;

x->y 是(除非被邪恶的人覆盖)(*x).y 的快捷方式。

a = pointer_to_x->y;

Take this small structure as given for my samples:

struct MyStruct {
   int y;
};

*x dereferences the pointer x. The return value is a reference to the object pointed to by x:

MyStruct* x = ...;
MyStruct xval = *x;
// or
MyStruct& xref = *x;
// read up on references to understand the latter

&x takes the address of x. The result is therefore a pointer to x.

MyStruct x;
MyStruct* pointer_to_x = &x;

x.y accesses a member named y in the object x.

MyStruct x;
x.y = 5;
int a = x.y + 2;
// a==7

(*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.

a = (*pointer_to_x).y;

x->y is (unless overridden by evil people) a shortcut for (*x).y.

a = pointer_to_x->y;
流殇 2024-11-07 11:50:01

*x 是取消引用运算符,如果您有 memoryAddress,则 *memoryAddress 是相应的对象。

相反,&x是引用运算符,如果你有一个object,那么&object提供内存地址。

对于访问变量,x->y 基本上是 (*x).y 的快捷方式,即取消引用然后访问成员。

*x is the dereference operator, if you have a memoryAddress, then *memoryAddress is the respective object.

The converse, &x is the reference operator, if you have an object, 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.

巴黎夜雨 2024-11-07 11:50:01

继续阅读!我将简要介绍几个,但在您真正理解一些基本概念之前,它可能仍然没有意义。

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

趁微风不噪 2024-11-07 11:50:01

*x取消引用 运算符。也就是说,它是 & 运算符的逆操作 - 它获取指针指向的内存的值。

& 是一个返回变量内存地址的运算符。

xy 表示访问x 的成员y

x -> y(*x).y 的同义词

*x is the dereference 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 of x

x -> y is a synonym for (*x).y

东走西顾 2024-11-07 11:50:01
  • &获取对象的内存地址
  • *获取内存地址指向的对象
  • 。访问对象
  • 的成员-> 从指向对象的指针访问成员
  • & 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文