In other words, why doesn’t the compiler just “know” that if the definition of a function is changed in a derived class, and a pointer to dynamically allocated memory of that derived class calls the changed function, then that function in particular should be called and not the base class’s?
In what instances would not having the virtual keyword work to a programmer’s benefit?
virtualkeyword tells the compiler to implement dynamic dispatch.That is how the language was designed.Without such an keyword the compiler would not know whether or not to implement dynamic dispatch.
The downside of
virtualor dynamic dispatch in general is that,