C++ 指针解引用运算符 ( (*) vs -> )
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4263796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pointer dereference operator ( (*) vs -> )
提问by Falmarri
Is there a general difference between doing
做之间有什么普遍的区别吗
(*ptr).method()
vs
对比
ptr->method()
I saw this question in a comment on another question and thought I would ask it here. Although I just remembered that pretty much every operator in C++ can be overloaded, so I guess the answer will depend. But in general, is there a difference between doing one versus the other?
我在另一个问题的评论中看到了这个问题,我想我会在这里问。虽然我只记得 C++ 中的几乎每个运算符都可以重载,所以我想答案将取决于。但总的来说,做一个和另一个有区别吗?
回答by Cheers and hth. - Alf
As "jamesdlin" already noted, the *and ->operators can be overloaded for class types.
正如“jamesdlin”已经指出的那样,可以为类类型重载*and->运算符。
And then the two expressions (*ptr).method()and ptr->method()can have different effect.
然后这两个表达式(*ptr).method()又ptr->method()可以有不同的效果。
However, for the built-inoperators the two expressions are equivalent.
但是,对于内置运算符,这两个表达式是等效的。
The ->operator is more convenient when you're following a chain of pointers, because .has higher precedence than *, thus requiring a lot of ungrokkable parentheses.
在->操作时,您遵循指针链,因为更方便.的优先级高于*,因此需要大量的ungrokkable括号。
Consider:
考虑:
pBook->author->snailMail->zip
versus
相对
(*(*(*pBook).author).snailMail).zip
回答by jamesdlin
For raw pointer types, they are the equivalent.
对于原始指针类型,它们是等效的。
And yes, for general types, the answer is indeed "it depends", as classes might overload operator*and operator->to have different behaviors.
是的,对于一般类型,答案确实是“视情况而定”,因为类可能会过载operator*并operator->具有不同的行为。
回答by wrongusername
Yes. ptr->method()is two characters shorter than (*ptr).method().
是的。ptr->method()比 短两个字符(*ptr).method()。
It is also prettier.
它也更漂亮。
回答by Kirill V. Lyadvinsky
C++ Standard 5.2.5/3:
C++ 标准 5.2.5/3:
If E1 has the type “pointer to class X,” then the expression E1->E2 is converted to the equivalent form (*(E1)).E2;
如果 E1 的类型为“指向类 X 的指针”,则表达式 E1->E2 将转换为等价形式 (*(E1)).E2;
For non-pointer values operators could be overloaded.
对于非指针值,运算符可以重载。
回答by Prasoon Saurav
But in general, is there a difference between doing one versus the other?
但总的来说,做一个和另一个有区别吗?
No! (unless ->and *are explicitly overloaded to perform different functions)
不!(除非->和*显式重载以执行不同的功能)
ptr->method()and (*ptr).method()are equivalent.
ptr->method()并且(*ptr).method()是等价的。
回答by Jonathan H
Sorry to dig this post, but even though the expressions in the OP are equivalent for raw pointer types, I think there is at least one important difference to be mentioned in C++, in addition to everything that has been said:
很抱歉挖这篇文章,但即使 OP 中的表达式对于原始指针类型是等效的,我认为除了已经说过的所有内容之外,我认为在 C++ 中至少有一个重要的区别要提到:
From Wikipedia (http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#cite_note-arrowptr-6):
来自维基百科(http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#cite_note-arrowptr-6):
The return type of operator->() must be a type for which the -> operation can be applied, such as a pointer type. If x is of type C where C overloads operator->(), x->y gets expanded to x.operator->()->y.
operator->() 的返回类型必须是可以应用-> 操作的类型,例如指针类型。如果 x 是 C 类型,其中 C 重载了 operator->(),则 x->y 被扩展为 x.operator->()->y。
This implies that ->is expected to return a dereferenceabletype, whereas *is expected to return a dereferencedtype, and therefore this "chaining" applies to ->only.
这意味着->预期会返回可取消引用的类型,而*预期会返回取消引用的类型,因此这种“链接”仅适用于->。
回答by AK.
The ->sequence serves as a visual indicator that it is pointing to something. Both operators do the exact same sequence of operations.
该->序列用作它指向某物的视觉指示器。两个运算符执行完全相同的操作序列。
回答by Alex Budovski
They are synonyms. The latter is a shorthand for the former.
它们是同义词。后者是前者的简写。

