C++: Replace raw pointers with shared and weak ptr

前端 未结 4 676
伪装坚强ぢ
伪装坚强ぢ 2021-01-13 16:17

I\'m facing a design issue in my program. I have to manage Nodes object which are part of a root ChainDescriptor.

Basically it looks like the following:



        
4条回答
  •  萌比男神i
    2021-01-13 17:01

    shared_ptr is owning smart pointer and weak_ptr is referencing smart pointer.

    So in your situation I think the ChainDescriptor should use shared_ptr (it owns the nodes) and Node should use weak_ptr for m_parent (it only references it) and shared_ptr for m_childs (it owns them).

提交回复
热议问题