SystemVerilog OOP Mastery · All levels
Handles, new(), and Construction Patterns: Concept Explained
Concept Explained for Handles, new(), and Construction Patterns.
Concept
In short: SystemVerilog class assignment copies handles, not object contents, so aliasing is the default behavior. Robust construction patterns use explicit new(), null checks, and clone/copy methods to avoid accidental shared state.
Class variables are references. When you do p2 = p1, both handles target the same heap object, and any mutation through one handle is visible through the other. This is efficient for passing transactions around, but dangerous when components expect private snapshots.
Constructors can accept parameters, for example new(string name = "pkt"), to lock in identity and defaults at allocation time. Good constructors keep objects valid immediately after new() and avoid requiring callers to remember many follow-up setup calls.
Null-handle checks are not optional in reusable infrastructure. Dereferencing null is a fatal runtime event, so production-quality scoreboards, predictors, and adapters protect method calls with if (h != null) or centralized ensure-created helpers.