From the course: Object-Oriented Programming with C++

Unlock this course with a free trial

Join today to access over 24,800 courses taught by industry experts.

Runtime polymorphism

Runtime polymorphism

- [Instructor] In our previous video, we saw how virtual functions let us write code that works with different types of reviews through Base class pointers. But what's actually happening behind the scenes? Let me explain how C++ makes this magic work. When this code runs, C++ needs to figure out which version of getRating to call, the Base Review version, the productReview version, or the RestaurantReview version. This decision happens at runtime, not when we compile the code. That's why we call it runtime polymorphism. This is different from regular function calls, where the compiler decides which function to call when compiling the code. With virtual functions, the decision is delayed until the program runs, allowing our code to work with types we might not even know about when writing it. C++ accomplishes this through a mechanism called dynamic dispatch. Note that implementation details may slightly vary across compilers, but the model we'll discuss is a good approximation of…

Contents