If you had the following:
class Animal{};
class Bird : public Animal{};
class Dog : public Animal{};
class Penguin : public Bird{};
class Poodle : public Dog{};
Does dynamic_cast
just check if one class is a derived class of another, or if one class is a base class of another? So if I had:
Bird* bird;
Animal* animal;
bird = dynamic_cast<Animal*>(bird);
animal = dynamic_cast<Bird*>(animal);
bird
would now point to an Animal
class, so that I can use bird->some_function();
and it will call the function in Animal
? And animal
now points to a Bird
class, so I can do animal->some_function();
and it will call some_function();
in Bird
?
I've been trying to figure out how the dynamic_cast
works, and the resources I've found online haven't been the most helpful. If someone can offer other insight into the functionality of dynamic_cast
and some instances in which it would be useful, I'd highly appreciate it.
bird
andanimal
are already pointers, but you're taking their addresses.