For what I'm working on, I'm making a class that holds a dynamic array of different sorts of pets. I have an abstract class of Animal, from which I have a few derived classes (Cat, Dog, Rabbit, Horse, etc.). I need to be able to add an instance of those derived classes to the end of the dynamic array. In the list object, I have a double pointer (Pet** pets) which is what is supposed to store the list of objects that have Pet as a base class. My thinking is that since Pet is the base class, then each pet pointer should be able to point to an object of type Cat or type Dog, etc.
I have a function in the PetArray class that is supposed to add an object to the end of the list, but I have a couple of issues:
- Every time this function is called, all entries in PetArray become the same as the last element I just tried to add.
- Later when I try to print info about each element in the array, the program crashes. I have a for loop that is supposed to print information about each element in the array, but my program always crashes on the first iteration of the loop when it's trying to access member data.
I am totally stuck here and am not sure how to fix what's wrong. Any help anybody can provide is greatly appreciated.
std::vector<Pet*>, I would recommend using that instead.std::vector<std::unique_ptr<Pet>>orstd::vector<std::shared_ptr<Pet>>PetArrayis actually being used.