unique_ptr and polymorphism

后端 未结 4 2043
深忆病人
深忆病人 2021-02-07 10:59

I have some code that currently uses raw pointers, and I want to change to smart pointers. This helps cleanup the code in various ways. Anyway, I have factory methods that retur

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-07 11:42

    If Push does not take owenrship, it should probably take reference instead of pointer. And most probably a const one. So you'll have

    Push(*number);
    

    Now that's obviously only valid if Push isn't going to keep the pointer anywhere past it's return. If it does I suspect you should try to rethink the ownership first.

提交回复
热议问题