SystemVerilog OOP Mastery · All levels

clone() and copy() Methods: Concept Explained

Concept Explained for clone() and copy() Methods.

Concept

In short: Robust testbench classes separate copy (state transfer) from clone (allocation plus copy), with inheritance-aware overrides and recursive member duplication.

A clean pattern is virtual copy(rhs) for field transfer and clone() for creating a new object then calling copy. This keeps allocation decisions and state-transfer decisions explicit.

In inheritance, base copy handles base fields, and derived copy first calls super.copy(rhs), then casts rhs to the derived type before touching derived fields. That cast prevents accidental access to fields that are not present in the runtime object.

This pattern is not just style: it prevents aliasing, preserves polymorphism, and gives scoreboards a predictable snapshot API.

Related topics