Question

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
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top