点 (.) 运算符和 -> 有什么区别?在 C++ 中?

2022-01-30 00:00:00 operators c++

What is the difference between the dot (.) operator and -> in C++?

解决方案

foo->bar() is the same as (*foo).bar().

The parenthesizes above are necessary because of the binding strength of the * and . operators.

*foo.bar() wouldn't work because Dot (.) operator is evaluated first (see operator precedence)

The Dot (.) operator can't be overloaded, arrow (->) operator can be overloaded.

The Dot (.) operator can't be applied to pointers.

Also see: What is the arrow operator (->) synonym for in C++?

相关文章