Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 vector of smart pointer

Suppose we have the following codes. We have following classes

  • Animal as AbstractClass
  • Dog and Bird which is subclass of Animal
  • Zoo which keeps all animals

_

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

I hope to replace vector of pointers by vector of smart pointers. i.e,

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

How do we change the code for Zoo and myTest? I find difficulty on updating the code, especially the method "AddAnimal" in Zoo class

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

bird and dog are different type

like image 475
user2434918 Avatar asked Feb 26 '15 02:02

user2434918


Video Answer


1 Answers

The behaviour of std::shared_ptr is very similar to that of a raw pointer with respect to the * and -> operators (in fact, the dereferencing operators are "forwarded" to the internal raw pointer stored by the std::shared_ptr). In particular, you can use a std::shared_ptr to a base class for virtual dispatching along a class hierarchy. For example, the code below does exactly what one assumes, namely calls the appropriate function at runtime:

#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
}

So, most of the time, it is enough to replace Animal* with std::shared_ptr<Animal>, and the code will just work. Things are a bit more complicated with std::unique_ptr, as the latter is a move-only type (you cannot copy it), so one must be more careful.

like image 110
vsoftco Avatar answered Oct 05 '22 23:10

vsoftco