When to use references vs. pointers

前端 未结 17 2500
一向
一向 2020-11-22 02:27

I understand the syntax and general semantics of pointers versus references, but how should I decide when it is more-or-less appropriate to use references or pointers in an

17条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 03:23

    For pointers, you need them to point to something, so pointers cost memory space.

    For example a function that takes an integer pointer will not take the integer variable. So you will need to create a pointer for that first to pass on to the function.

    As for a reference, it will not cost memory. You have an integer variable, and you can pass it as a reference variable. That's it. You don't need to create a reference variable specially for it.

提交回复
热议问题