Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I was learning about C++ pointers and the -> operator seemed strange to me. Instead of ptr->hello(); one could write (*ptr).hello(); because it also seems to work, so I thought the former is just a more convenient way.

Is that the case or is there any difference?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
488 views
Welcome To Ask or Share your Answers For Others

1 Answer

The -> operator is just syntactic sugar because (*ptr).hello() is a PITA to type. In terms of the instructions generated at the ASM level, there's no difference. In fact, in some languages (D comes to mind), the compiler figures everything out based on type. If you do ptr.hello(), it just works, because the compiler knows that ptr is a pointer and doesn't have a hello() property, so you must mean (*ptr).hello().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...