Arrow Operator vs. Dot Operator [closed]

北城以北 提交于 2019-11-27 11:28:40

The 'arrow' operator is syntactic sugar. bar->member is the same as (*bar).member. One reason for the difference is maintainability. With the arrow operator distinct from the dot operator, it becomes much easier to keep track of which variables are pointers and which are not. It might be possible to always use . and have the compiler try to do the right thing, but I doubt that would make the language simpler. Trusting the compiler to interpret what you meant instead of what you literally wrote usually turns out badly.

No, it would not be easier to eliminate -> from the language, for the simple reason that megatons of code would have to be rewritten if it were. However one could define that p.x is equivalent to p->x if p is a pointer. That would be a backwards-compatible change because that code is currently illegal.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!