My understanding of C++ arrays is that you can't allocate an array of abstract class objects since C++ doesn't know how to allocate memory for a yet-to-be-decided class type.
I put together a little example that confuses me a bit, so wanted to ask a bit more
#include <iostream>
class Animal {
public:
  virtual void hello() {}
};
class Dog : public Animal {
public:
  void hello() { std::cout << "woof!" << std::endl; }
};
class Cat : public Animal {
public:
  void hello() { std::cout << "meow" << std::endl; }
};
int main() {
  Dog d;
  d.hello(); // prints "woof!"
  Cat c;
  c.hello(); // prints "meow"
  // how are we allowed to create an array of abstract class?
  // doesn't c++ need to know how to allocate memory for any abstract
  // class in order to do this?
  Animal creatures[5];
  creatures[0] = d;
  creatures[1] = c;
  creatures[4] = d;
  // prints "6Animal"
  std::cout << typeid(creatures[0]).name() << std::endl;
  // this appears to call the Animal hello(), which does nothing
  creatures[1].hello();
}
Questions
- How is C++ able to allocate memory for this array? Why doesn't it complain?
- It appears something about this not failing is due to treating all the objects as Animals, ie: not properly doing polymorphism. What exactly is going on, and why? Do I just have to allocate for a list of pointers to do this properly instead?
Thanks!
 
    