mercredi 4 novembre 2015

Clarifying the term "object ownership" [duplicate]

This question already has an answer here:

I have seen many uses of the term "object ownership", but I am not confident that I understand correctly and fully what it means. I believe that an answer to the following two questions would clarify the term. (I know that a post that asks two questions is usually considered too broad, but hope that an exception can be made when the questions are aiming to clarify a single concept.)

Firstly, is "object ownership" synonymous with "the responsibility to free the resources held by the object" or there is more to it?

Secondly, suppose that we are managing an object x. Suppose that y is another object that stores a pointer to x as its data. Furthermore, there are other objects that store a pointer to x, but we know that y will outlive all such other objects. In this case, is it true that:

  1. y should be made an exclusive owner of x and it should hold a unique_ptr to x.
  2. All other objects (both their data and function members) should use regular pointers and references to x.

P.S. It is interesting that the tag ownership does not have a definition.

Aucun commentaire:

Enregistrer un commentaire