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

前端 未结 4 641
伪装坚强ぢ
伪装坚强ぢ 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条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-13 16:59

    The usual implementation would be for each node to have strong reference to its child (i.e. keeps them alive), and each child to have a weak reference back to the parent.

    The reason for this is to avoid circular references. If only strong references were used, then you'd have a situation where the parent refcount never drops to zero (because the child has a reference), and the child refcount never drops to zero (because the parent has a reference).

    I think your ChainDescriptor class is okay to use strong references here though.

提交回复
热议问题