C++11 vector 的智能指针

标签 c++ c++11 abstract-class smart-pointers

假设我们有以下代码。我们有以下类(class)

  • 作为抽象类的动物
  • Dog 和 Bird 是 Animal 的子类
  • 饲养所有动物的动物园

_

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)
    {
        ...
    }
    ...
}

我希望用智能指针 vector 代替指针 vector 。即,

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

我们如何更改 Zoo 和 myTest 的代码? 我发现更新代码有困难,尤其是 Zoo 类中的“AddAnimal”方法

auto bird = std::make_shared<Bird>();
auto dog = std::make_shared<Dog>();
myZoo.AddAnimal(bird);
myZoo.AddAnimal(dog);

鸟和狗是不同的类型

最佳答案

std::shared_ptr 的行为与关于 * 的原始指针非常相似和 ->运算符(实际上,取消引用运算符被“转发”到 std::shared_ptr 存储的内部原始指针)。特别是,您可以使用 std::shared_ptr到一个基类,用于沿类层次结构进行虚拟调度。例如,下面的代码完全符合我们的假设,即在运行时调用适当的函数:

#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 的事情有点复杂,因为后者是只能移动的类型(你不能复制它),所以要更加小心。

关于C++11 vector 的智能指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28733385/

相关文章:

C++。从 double 重新解释为 unsigned char*

c# - 面向 C/C++ 用户的 C# 简介

c++ - 初始化和终止 Qt 应用程序的正确方法?

objective-c - 为什么不能在 objective-c 中创建抽象类

java - 抽象类 : Why newInstance() is not giving compilation error but constructor call gives error?

c++ - 检测内存页面大小

c++ - 奇怪的错误 - std::regex 只匹配前两个字符串

c++ - 在文件范围内声明互斥锁安全吗?

C++类型转换奇怪

java - 抽象类的简单Q迭代器<Object E>