Home > Blockchain >  Is it reasonable to use a static member function within another static member function contained wit
Is it reasonable to use a static member function within another static member function contained wit

Time:02-05

I am attempting to reorganize some code such that it is self-documenting. If I have a class that I don't intend to instantiate, but rather want to call to perform various tasks all related to the same idea, can I create simple static member functions that are then called by later static member functions (Or tasks in my case)?

class foo;
  
  static protected bit [7:0] Data_A;
  static protected bit [7:0] Data_B;

  static task bar();
    /* Use to print out info. in Data_A and Data_B. */
  endtask : bar

  static task FooBar();
    /* Perform some manipulations on Data_A and Data_B. */
    bar();
  endtask : FooBar

endclass : foo

Then in my program I would do something like:

program automatic main();
  initial
    begin
    foo::FooBar();
    $finish;
    end
endprogram

Does this seem sound? Thank you for your time. I have been reading the IEEE System-Verilog standard, but this situation does not seem to come up.

Edit: Do I need to use the "this" keyword, or is that reserved for instantiated classes to refer to the self?

CodePudding user response:

It is perfectly appropriate to be able to call one static method of a class from another. You do not use the this prefix to call a static method. You could prefix your call with foo::bar() just to make it clearer to the reader that you are calling a static method, but the language does not need it.

Any class you do not want constructed should be declared as

virtual class foo;

You can even go further by declaring the constructor as

local function new; endfunction

That keeps people from extended it and constructing, but that might be an overkill.

And finally a suggestion: what you are doing might be better suited for a package

package automatic foo;
  
  bit [7:0] Data_A;
  bit [7:0] Data_B;

  task bar();
    /* Use to print out info. in Data_A and Data_B. */
  endtask : bar

  task FooBar();
    /* Perform some manipulations on Data_A and Data_B. */
    bar();
  endtask : FooBar

endpackage : foo


module automatic main();
  initial
    begin
    foo::FooBar();
    $finish;
    end
endmodule
  •  Tags:  
  • Related