指针取消引用运算符((*)vs - >) [英] Pointer dereference operator ( (*) vs -> )

查看:133
本文介绍了指针取消引用运算符((*)vs - >)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行之间有一般差异

(*ptr).method()

vs

ptr->method()



我在另一个问题的评论中看到这个问题,我想在这里问。虽然我只是记得几乎每个操作符在C ++可以重载,所以我想答案将取决于。

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?

推荐答案

由于jamesdlin已经提到, * - > 运算符可以重载类类型。

As "jamesdlin" already noted, the * and -> operators can be overloaded for class types.

然后两个表达式(* ptr).method() ptr-> gt()具有不同的效果。

And then the two expressions (*ptr).method() and ptr->method() can have different effect.

但是,对于内建的运算符,两个表达式是等价的。

However, for the built-in operators 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.

请考虑:

pBook->author->snailMail->zip

(*(*(*pBook).author).snailMail).zip

这篇关于指针取消引用运算符((*)vs - >)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆