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