Ready-to-run snippets

Code Examples

Ready-to-run Verilog, SystemVerilog, and UVM examples. Click any to open it live in the IDE.

← All examples

'this' keyword: The Classic Case: A Shadowed Constructor Argument

SystemVerilog Classes & OOP

Runnable example from the SystemVerilog 'this' keyword tutorial (The Classic Case: A Shadowed Constructor Argument)

Open in IDE
testbench.sv
class Packet;
	bit [31:0] addr;

	function new (bit [31:0] addr);
//		addr = addr;          //  Which addr should get assigned ?

		this.addr = addr;     //  addr variable in Packet class should be
		                      //  assigned with local variable addr in new()
	endfunction
endclass

module tb;
  initial begin
    Packet pkt = new (32'hdead_beef);
    $display ("addr=0x%0h", pkt.addr);
  end
endmodule
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.