Collect all Sheep in the Zoo using dynamic cast (the recommended way)
| 25 | |
| 26 | // Collect all Sheep in the Zoo using dynamic cast (the recommended way) |
| 27 | std::vector<Sheep*> Zoo::herd() const |
| 28 | { |
| 29 | std::vector<Sheep*> sheep; |
| 30 | for (auto animal : m_animals) |
| 31 | { |
| 32 | auto* casted{ dynamic_cast<Sheep*>(animal.get()) }; |
| 33 | if (casted) |
| 34 | { |
| 35 | sheep.push_back(casted); |
| 36 | } |
| 37 | } |
| 38 | return sheep; |
| 39 | } |
| 40 | |
| 41 | /* |
| 42 | // Collect all Sheep in the Zoo using the typeid() operator and a static cast |