Copy function for Parameterized Class

I am struggling to write a typical copy function of a parameterized class.


class seq_item #(parameter int AWIDTH=10, parameter int DWIDTH=16);
  rand bit[AWIDTH-1 :0] addr;
  rand bit[DWIDTH-1 :0] data;

  virtual function seq_item copy(seq_item to=null);
    seq_item tmp;
    if (to == null) tmp = new();
    else tmp = to;
    tmp.addr = this.addr;
    tmp.data = this.data;
    return tmp;
  endfunction
endclass

If I call the copy function from a object which is created by overriding the default seq_item parameter values, it causes a compilation error.

In reply to mussoi:

Please use code tags making your code easier to read. I have added them for you.

You want to make sure that you use the parameterized version of your class in the copy function:


class seq_item #(parameter int AWIDTH=10, parameter int DWIDTH=16);

  typedef seq_item#(AWIDTH,DWIDTH) seq_item_t;

  rand bit[AWIDTH-1 :0] addr;
  rand bit[DWIDTH-1 :0] data;
 
  virtual function seq_item_t copy(seq_item_t to=null);
    seq_item_t tmp;
    if (to == null) tmp = new();
    else tmp = to;
    tmp.addr = this.addr;
    tmp.data = this.data;
    return tmp;
  endfunction
endclass