Parameterized class: specialization

In reply to NiLu:

I don’t know what you’re doing in your get_next_complex/real(…) functions, but if you were to call any methods on the object on the argument you pass to them, then you don’t need to care whether that argument is real or complex as long as you’re using virtual methods:


virtual class number;
  virtual function void do_something();
endclass

class real_number extends number;
  virtual function void do_something();
    $display("doing something on a real");
  endfunction
endclass

class complex_number extends number;
  virtual function void do_something();
    $display("doing something on a complex");
  endfunction
endclass

This means that whenever you call one of these functions on a handle of type number the one corresponding to the appropriate type will get called:


function void get_next_number(number num);
  num.do_something();
endfunction


// ...
real_number real_num = new();
get_next_number(real_num);

complex_number complex_num = new();
get_next_number(complex_num );

The first call will print “doing something on a real”, while the second will print “doing something on a complex”. You don’t have to do any casting this way.