Should I store entire objects, or pointers to objects in containers?

后端 未结 10 2085
忘掉有多难
忘掉有多难 2020-11-27 09:31

Designing a new system from scratch. I\'ll be using the STL to store lists and maps of certain long-live objects.

Question: Should I ensure my objects have copy co

10条回答
  •  日久生厌
    2020-11-27 10:20

    You seem to have a good grasp of the difference. If the objects are small and easy to copy, then by all means store them.

    If not, I would think about storing smart pointers (not auto_ptr, a ref counting smart pointer) to ones you allocate on the heap. Obviously, if you opt for smart pointers, then you can't store temp stack allocated objects (as you have said).

    @Torbjörn makes a good point about slicing.

提交回复
热议问题