When to use references vs. pointers

前端 未结 17 2684
一向
一向 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:32

    It is not a matter of taste. Here are some definitive rules.

    If you want to refer to a statically declared variable within the scope in which it was declared then use a C++ reference, and it will be perfectly safe. The same applies to a statically declared smart pointer. Passing parameters by reference is an example of this usage.

    If you want to refer to anything from a scope that is wider than the scope in which it is declared then you should use a reference counted smart pointer for it to be perfectly safe.

    You can refer to an element of a collection with a reference for syntactic convenience, but it is not safe; the element can be deleted at anytime.

    To safely hold a reference to an element of a collection you must use a reference counted smart pointer.

提交回复
热议问题