| martianxx (42) | |||
|
My question probably has a simple answer but I am not 100% sure on the syntax of replicating the following scenario. Say I have 3 classes. The first class is called Animal. The other two classes both inherit from Animal and are called Cat and Dog. Next say I wanted to store an array of Animal objects but I want to be able to store both cats and dogs in the same array. I want to store 10 animals but in an unknown order of Cat and Dog objects. What is the syntax for declaring an array such as the one described above? My first guess would be something along the lines of the following:
I just read that a pointer to a derived class is type compatible with a pointer to its base class although I am still unsure of the syntax | |||
|
Last edited on
|
|||
| Cubbi (1927) | |
To get your code to work, you could useAnimal* animal_array[10];Practical solutions are std::array<std::unique_ptr<Animal>, 10> animal_array;or boost::ptr_array<Animal, 10> animal_array;(although vectors are often more useful) | |
|
Last edited on
|
|