Part 2 · OOP for Verification · Intermediate

Classes, Objects & Handles

Hub — class declaration, object construction, handle semantics, constructors, copy depth, this/scope, and object lifetime.

Overview

Everything in a class-based testbench reduces to three ideas: a class is a blueprint, an object is a runtime instance built by new(), and a handle is a reference variable that points at an object. Most beginner bugs — null dereferences, two scoreboards 'seeing' each other's data, transactions corrupted after being sent — come from confusing the handle with the object it points to. This topic builds the memory model precisely, then layers constructors, copy semantics, scoping, and lifetime on top.

Sub-topics

  1. Class Declaration & Objects — properties, methods, new(), the construction flow.

  2. Handles vs Objects — reference semantics, null dereference, aliasing, garbage collection.

  3. Constructors & super.new() — arguments, defaults, chaining, construction order.

  4. Shallow vs Deep Copy — new src semantics, shared sub-object bugs, copy()/clone().

  5. this, Scope & Nested Classes — disambiguation, class scope resolution, typedef forward declarations.

  6. Object Lifetime & Memory — collectability, retention leaks, handle-reassignment interview question.

diagram
CLASSES & HANDLES — topic map

  class txn;          ← blueprint (no memory yet)
       │
       │  txn t;      ← handle, value = null
       │  t = new();  ← object built on heap, handle points to it
       ▼
  ┌─────────┐    ┌──────────────────────┐
  │ handle t│───►│ OBJECT  addr data ...│   1. declaration & new()
  └─────────┘    └──────────────────────┘
       │
       ├── t2 = t;         aliasing            2. handles vs objects
       ├── new(args)       constructor chain   3. constructors
       ├── t2 = new t;     shallow copy        4. copy depth
       ├── this.addr       scope resolution    5. this & scope
       └── t = null;       collectable?        6. lifetime & memory

Key takeaways

  • A class is a type; an object is heap memory; a handle is a pointer-like reference to that memory.

  • Handle assignment never copies an object — it copies the reference.

  • Objects live as long as any handle (including ones hiding in queues and mailboxes) still reaches them.