문제

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
도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top