Calling function in class

Hi All
I have a class . there is functionA in my class. I want to record the time only when function is called first time.
class A
function A
do_something…
endfunction
endclass
is there a good way to do that?
thanks

In reply to timag:

You could use a static variable in the class;


class A;
static bit first;

function my_func;
  if(first==0) begin
    $display("Time on first call is %d",$time);
    first = 1;
  end
endfunction

endclass;

In reply to KillSteal:

why need use static?
thanks

In reply to timag:

Maybe I misunderstood the question, but I was thinking of a situation where you could have multiple instances of the class.

If you have multiple instances of the class, and if you want to record time every first call of the function in every instance of the class, then, you don’t need static.

But, if you only want to record time the very first time the function is called across multiple instances of the class, then you need static.


module top;
 A a1,a2; 

  initial begin
    a1=new(); a2=new();

    a1.my_func() ; // With static , record time here. Without static record time here.
    a1.my_func(); //No record of time.

    a2.my_func();  // With static, no record of time here. Without static, record time here too.
    a2.my_func();  // No record of time.

  end

endmodule