0

so I have this code:

Base* objbase = new Derived();

//perform a downcast at runtime with dynamic_cast
Derived* objDer = dynamic_cast<Derived*>(objBase);

if(objDer)//check for success of the cast
    objDer->CallDerivedFunction();

This is a snippet of code for a cast operators section in my book.

Now why do I have this, I don't understand what's the point of having to dynamically cast a pointer to a base object pointing to a Derived object; For me, that's something to do with polymorphism giving us the ability to do objBase->DeriveClassFunction(), but I don't really know.

In the first place why does it do this: Base* objbase = new Derived();, and then why does it cast a base object pointer to a Derived again, I can't quite figure out why.

Thanks in advance.

Alex
  • 840
  • 7
  • 23
  • 1
    Note: Inheritance is best used with generalized `virtual` functions so that the users of the object do not need to know what the type of the object is. `dynamic_cast`ing does not scale well. Eventually you find yourself with more checking to ensure you have the right object than you have code that actually solves the problem at hand. See the [Liskov Substitution Principle](https://stackoverflow.com/questions/56860/what-is-an-example-of-the-liskov-substitution-principle) for a tool to help you detect when you're following this bad path. – user4581301 Apr 24 '20 at 19:40

1 Answers1

2

That code snippet is just a demonstration of what's possible. It describes a tool, what you do with this tool is up to you. A slightly bigger example might be:

class Animal {
    void Feed();
};
class Cat : public Animal { /*...*/ };
class Dog : public Animal {
    // Only dogs need to go out for a walk
    void WalkTheDog();
};

void Noon(Animal* pet)
{
    // No matter what our pet is, we should feed it
    pet->Feed();

    // If our pet is a dog, we should also take it out at noon
    Dog* dog = dynamic_cast<Dog*>(pet);
    if(dog) // Check if the cast succeeded
        dog->WalkTheDog();
}

Noon(new Cat()); // Feed the cat
Noon(new Dog()); // Feed the dog and take him out

Notice that every animal has the Feed() function, but only dogs have the WalkTheDog() function, so in order to call that function, we need to have a pointer to a dog. But it would also be quite a waste to copy the Noon() function for both types, especially if we might later add even more animals. So instead, the Noon() function works for any kind of animal, and does only the dog-specific things only if the animal is actually a dog.

Bizzarrus
  • 1,194
  • 6
  • 10
  • So, essentially it just permits type checking at runtime and nothing else? – Alex Apr 24 '20 at 18:30
  • 1
    Well, it *does* the type checking at runtime, yes. It allows you to access functions/members of the derived class from a base pointer. Like, pet->WalkTheDog() would actually cause a compile time error because the animal class does not have such a function at all. (But you could also just use it only for type checking purpose, if needed) – Bizzarrus Apr 24 '20 at 18:33