Tuesday 2 May 2017

"this" in system verilog

Many times, we uses the variable name of the method's argument which is similar to the class's property. To identify the current object's property "this" keyword is used in object oriented programming. The "this" keyword denotes a predefined object handle that refers to the object that was used to invoke the subroutine that "this" is used within.

Example:
class check_id;
  int incr_id = 0;
  function void set_incr(int incr_id);
    this.incr_id = incr_id;
  endfunction


  function int get_incr();
    return this.incr_id;
  endfunction
endclass

module top();
  check_id id_1;
  initial begin
    id_1 = new();
    id_1.set_incr(1);
    $display("First=%0d",id_1.get_incr());
    id_1.set_incr(2);
    $display("Second=%0d",id_1.get_incr());
  end
endmodule


Output:
First=1
Second=2

"this" keyword can not be used in static class methods as it refers to the instance of the class. It will give compilation error.

No comments:

Post a Comment