1

Which of the two copy functions are better?

A. Using reference to a function parameter:

function void copy(ref MyClass copyme);
    MyClass copyme = new this;
endfunction

B. Returning a newly instantiated copy:

function MyClass copy();
    return new this;
endfunction
e19293001
  • 2,783
  • 9
  • 42
  • 54

1 Answers1

6

Something like A is preferred for copy(). Use clone() for create then copy. Copy and clone are usually written as

class Myclass;
  int A;
  function void copy(Myclass rhs)
    this.A = rhs.A;
  endfunction
  virtual function Myclass clone();
    clone = new();
    clone.copy(this);
  endfunction
endclass

Note that clone is virtual, copy is non-virtual. Also, you do not need to pass class handles as ref arguments - class variables are already references.

Greg
  • 18,111
  • 5
  • 46
  • 68
dave_59
  • 39,096
  • 3
  • 24
  • 63