When is it preferable to store data members as references instead of pointers?

前端 未结 5 2397
耶瑟儿~
耶瑟儿~ 2021-02-20 16:33

Let\'s say I have an object Employee_Storage that contains a database connection data member. Should this data member be stored as a pointer or as a reference?

5条回答
  •  粉色の甜心
    2021-02-20 16:45

    It is almost never prefereable to store references as data members, and a lot of the time it is impossible. If the objects must be assignable (as they must to be stored in a standard library container), references cannot be used. Also, references cannot be reseated, so once a reference is initialised with an object, it cannot be made to refer to a different object.

    See this question Should I prefer pointers or references in member data? for a more detailed discussion of the issue.

提交回复
热议问题