SystemVerilog OOP Mastery · All levels

Virtual vs Nonvirtual Traps

Forgetting virtual in base classes silently disables dynamic dispatch through base handles, so framework code executes base behavior instead of protocol-specific overrides.

Overview

Forgetting virtual in base classes silently disables dynamic dispatch through base handles, so framework code executes base behavior instead of protocol-specific overrides.

This bug is painful because unit tests on concrete classes pass. Failure appears only when the framework stores everything as base handles and calls hook methods that were intended to be polymorphic.\n\nThe fix is contractual: mark extension points virtual in the base class, keep signatures identical, and use override checks where possible. Treat nonvirtual methods as intentionally final utility methods, not callbacks.

In this topic

diagram
1. Concept Explained
2. Code Examples
3. SystemVerilog vs C++/Java
4. Pitfalls
5. Exercises
6. Interview Questions

Read the concept first, study the code examples, compare against C++/Java, then test yourself with the exercises and interview questions.

OOP bug root-cause decision tree

diagram
ROOT CAUSE TREE FOR OOP FAILURES

symptom: test behavior mismatch
                 |
           reproducible?
            /       \
          no         yes
          |           |
   check race/time   inspect object state
                      /        |        \
                 null handle  aliasing  wrong override
                    |            |           |
               construct path  clone policy  virtual+factory map
                    \           |           /
                     +------ implement fix ------+

Localize the failing object lifecycle before broad refactoring.

Related topics