SystemVerilog OOP Mastery · All levels
this, Scope Resolution, and Automatic Lifetime: Code Examples
Code Examples for this, Scope Resolution, and Automatic Lifetime.
Code examples
Using this in constructor assignment
systemverilog
class packet;
string name;
int unsigned size;
function new(string name, int unsigned size = 0);
this.name = name;
this.size = size;
endfunction
endclassShows explicit object-field assignment when argument names shadow member names.
Automatic locals versus shared member state
systemverilog
class counter;
int shared_count;
task bump(int n);
int local_before; // automatic local for each call
local_before = shared_count;
#1;
shared_count = local_before + n;
endtask
endclass
module life_demo;
counter c = new();
initial fork
c.bump(1);
c.bump(2);
join
endmoduleEach task call gets its own local_before, but both calls still race on shared_count because it is object state.
Scope resolution with class static members
systemverilog
class id_gen;
static int next_id = 0;
int my_id;
function new();
my_id = id_gen::next_id;
id_gen::next_id++;
endfunction
endclassUses class scope resolution (::) for shared class-level state distinct from per-object members.