C++11 向量的智能指针

C++11 vector of smart pointer

假设我们有以下代码。我们有以下 classes

_

class Animal
{
public:
    Animal();
    void HasWings() = 0;
};

class Bird : public Animal
{
public:
    Bird() : Animal() {}
    void HasWings() override { return true; }
};

class Dog : public Animal
{
public:
    Dog() : Animal() {}
    void HasWings() override { return false; }
};

class Zoo
{
public:
    Zoo() {}
    void AddAnimal(Animal* animal) { _animals.push_back(animal); }
    ...
    std::vector<Animal*> _animals;
};

void myTest()
{
    Zoo myZoo;
    Bird* bird = new Bird();
    Dog* dog = new Dog();

    myZoo.AddAnimal(bird);
    myZoo.AddAnimal(dog);

    for (auto animal : myZoo._animals)
    {
        ...
    }
    ...
}

我希望用智能指针向量代替指针向量。即

std::vector<std::shared_ptr<Animal>> _animals;

我们如何更改 Zoo 和 myTest 的代码? 我发现更新代码有困难,尤其是 Zoo class

中的方法 "AddAnimal"
auto bird = std::make_shared<Bird>();
auto dog = std::make_shared<Dog>();
myZoo.AddAnimal(bird);
myZoo.AddAnimal(dog);

鸟和狗是不同的类型

std::shared_ptr 的行为与原始指针在 *-> 运算符方面的行为非常相似(事实上,解引用运算符是 "forwarded" 到 std::shared_ptr 存储的内部原始指针)。特别是,您可以使用 std::shared_ptr 到基础 class 沿 class 层次结构进行虚拟调度。例如,下面的代码完全符合我们的假设,即在运行时调用适当的函数:

#include <iostream>
#include <memory>
#include <vector>

struct Base
{
    virtual void f() { std::cout << "Base::f()" << std::endl;}
    virtual ~Base() = default; // to silence -Wall warnings
};

struct Derived: Base
{
    void f() override { std::cout << "Derived::f()" << std::endl;}
};

int main()
{
    std::vector<std::shared_ptr<Base>> vsp; // access Derived via shared_ptr to Base

    auto base = std::make_shared<Base>();
    auto derived = std::make_shared<Derived>();

    vsp.push_back(base);
    vsp.push_back(derived);

    for(auto&& elem: vsp)
        elem->f(); // virtual dispatch
}

因此,大多数情况下,将 Animal* 替换为 std::shared_ptr<Animal> 就足够了,代码将正常运行。 std::unique_ptr有点复杂,因为后者是只能移动的类型(你不能复制它),所以要更加小心。