TRT
Sign in

Core · Module 6 · Lesson 2 of 6

Virtual Functions and Dynamic Dispatch

Call one interface and reach the right implementation at run time, using virtual functions, override and the vtable that makes it work.

Pro — members onlyAbout 11 minutes

By the end you can

  • Explain how a virtual call reaches the derived implementation
  • Use override to let the compiler check your signatures
  • Predict the output of a base-pointer call to a virtual function

Builds on Inheritance: Reusing and Extending Types.

01 · A question first

You hold a list of library items: books, DVDs, magazines. One loop must print each item's loan period, but the rule differs per kind. How does one call, `item->loanDays()`, reach three different answers without a single if or switch?

Make a guess before you read on.

02 · The idea

Members only

Unlock the full lesson

This lesson is part of the membership. Join to read it in full, along with every other premium deep dive.

What's inside

  • Start with the problem
  • How the idea works
  • Why it is correct
  • What it costs
  • Pitfalls and variations
  • Analogy and a second dispatch check
  • Where it is used

03 · Practice

Something wrong with this lesson? Report a problem