I have a class Animal, with two derived classes Cat and Dog respectively. I also have a class House which contains two lists as attributes: a list of every cat in the house ( std::vector<Cat*>
), and a list of every dog ( vector<Dog*>
).
My goal is to code a function in House which clears a list of animals ( void House::clearList( std::vector<Animal*> )
). I'd like to avoid duplicating code as much as possible. Hence, creating one function for both list would be optimal.
I'd like to call this function in the destructor of House :
class Animal{};
class Dog : public Animal{};
class Cat : public Animal{};
class House{
private :
std::vector<Dog*> Dogs;
std::vector<Cat*> Cats;
public :
~House(){
clearList(dogs);
clearList(cats);
}
clearList(std::vector<Animal*> animals){
for (auto& animal : animals){
animal=nullptr;
delete animal;
}
}
};
The compiler displays :
error: no matching function for call to 'House::clearList(std::vector&)' note: no known conversion for argument 1 from '
std::vector<Cat*>
' to 'std::vector<Animal*>
'
I can't use shared_ptr
nor unique_ptr
in my code.
How can I use polymorphism to solve this?
Thanks
Aucun commentaire:
Enregistrer un commentaire