SystemVerilog OOP Mastery · All levels

clone() and copy() Methods

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

Overview

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 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.

Deep copy versus shallow copy

diagram
DEEP VS SHALLOW COPY

source txn s
   |
   +-- payload_handle -----> payload object @0x3000

shallow copy: txn t = s;
   t.payload_handle --------^ (same nested object)

deep copy: t.copy(s);
   t.payload_handle -----> payload object @0x4100 (new clone)

Shallow copy duplicates handles. Deep copy duplicates reachable object graph.

Related topics