ptr->hello(); /* VERSUS */ (*ptr).hello();

前端 未结 6 994
无人及你
无人及你 2020-12-02 01:41

I was learning about C++ pointers and the -> operator seemed strange to me. Instead of ptr->hello(); one could write (*ptr).hello();

6条回答
  •  春和景丽
    2020-12-02 02:23

    The only reason to have the '->' operator is to make it more convenient and save errors like:

    *ptr.hello();
    

    Because it is so easy to forget the parenthesis.

提交回复
热议问题