fork download
  1. #include <iostream>
  2. #include <list>
  3. using namespace std;
  4.  
  5. class Animal {
  6. public:
  7. virtual void speak() = 0;
  8. string name;
  9. };
  10.  
  11. class Dog: public Animal {
  12. public:
  13. Dog(string n) { this->name=n; }
  14. void speak() { cout<<this->name<<" says WOOF!"<<endl; }
  15. };
  16.  
  17. class AnimalQueue {
  18. list<Dog> dogs;
  19. public:
  20. void enqueue(Animal* a) {
  21. Dog * d = dynamic_cast<Dog*>(a);
  22. dogs.push_back(*d);
  23. }
  24. Dog* dequeueDog() {
  25. Dog * d = &(dogs.front());
  26. dogs.pop_front();
  27. return d;
  28. }
  29. };
  30.  
  31. int main() {
  32. // Set up
  33. AnimalQueue q;
  34. Dog * d;
  35. d = new Dog("Rex");
  36. q.enqueue(d);
  37.  
  38. // Try with dog-specific command
  39. d = q.dequeueDog();
  40. cout<<d->name<<endl;// Prints "Rex"
  41. d->speak(); // Crashes?!
  42.  
  43. return 0;
  44. }
Runtime error #stdin #stdout #stderr 0s 3276KB
stdin
Standard input is empty
stdout
Rex
stderr
pure virtual method called
terminate called without an active exception