Difficulty: Medium · Puzzle 2 of 6 · Topic: Parameterized Class Puzzles
Do these two handles share the same static counter or maintain two counters?
class pkt #(int W = 8);
static int created = 0;
function new();
created++;
endfunction
endclass
typedef pkt#(32) pkt32_t;
initial begin
pkt#(32) a = new();
pkt32_t b = new();
$display("pkt#(32)::created=%0d", pkt#(32)::created);
$display("pkt32_t::created=%0d", pkt32_t::created);
endA typedef alias does not create a new specialization.
1) `pkt32_t` is only an alias for exactly `pkt#(32)`.
2) Both objects increment the same specialization static `created`.
3) Both displays print 2.Answer: They share one counter; both lines print 2 because typedef alias points to same specialization.
Mistaking typedef for a new derived type causes wrong assumptions about static sharing.
Would `pkt#(16)::created` change in this snippet?