unique_ptr and polymorphism

后端 未结 4 2033
深忆病人
深忆病人 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:39

    Here's a polymorphism example using unique pointer:

    vector> creatures;
    
    creatures.emplace_back(new Human);
    creatures.emplace_back(new Fish);
    
    unique_ptr> pLog(new vector());
    
    for each (auto& creature in creatures)
    {
        auto state = creature->Move(*pLog);
    }
    

提交回复
热议问题