Esempio n. 1
0
int main() {
  Bird b; 
  Cat c;
  Pet* p[] = { &b, &c, };
  for(int i = 0; i < sizeof p / sizeof *p; i++)
    cout << p[i]->type() << " je "
         << p[i]->eats()->foodType() << endl;
  // Funkcja moze zwrocic dokladny typ:
  Cat::CatFood* cf = c.eats();
  Bird::BirdFood* bf;
  // Funkcja nie moze zwrocic dokladnego typu:
//!  bf = b.eats();
  // Trzeba rzutowac w dol:
  bf = dynamic_cast<Bird::BirdFood*>(b.eats());
} ///:~
Esempio n. 2
0
int main() {
  Bird b; 
  Cat c;
  Pet* p[] = { &b, &c, };
  for(int i = 0; i < sizeof p / sizeof *p; i++)
    cout << p[i]->type() << " eats "
         << p[i]->eats()->foodType() << endl;
  // Can return the exact type:
  Cat::CatFood* cf = c.eats();
  Bird::BirdFood* bf;
  // Cannot return the exact type:
//!  bf = b.eats();
  // Must downcast:
  bf = dynamic_cast<Bird::BirdFood*>(b.eats());
} ///:~