Importance of the clone( ) method

In reply to NiLu:

I do not understand the claims about copy() here. I agree with the example code, but in UVM the non-virtual copy() method calls the virtual do_copy() method. So the do_copy of the derived class gets called, and hence its members do get copied?
For example:


function void doit();
base B1,B2;
ex E1,E2;
E1=new;
E1.f1=123;
E1.f2=456;
B1=E1;
E2=new;
B2=E2;
B2.copy(B1);
$display("E2.f1=%0d",E2.f1);
$display("E2.f2=%0d",E2.f2);
endfunction

There is quite a bit of confusion in this thread on copy vs clone.

The problem with the quoted example is that the child class type must be known at compile time. Doing “E2=new; B2=E2;” means that B2 is pointing to type ex, so ex::do_copy() will get run as intended.

But what if we don’t know the child class type? After all, that is the whole point. We have only a handle to an object whose specific subclass is not known at compile time. The copy() method does not work in this case. Here’s a few examples:


///// option 1: no good because you must know type of rhs  
//    E2=new;                                                    
//    B2=E2;                                         
///// option 2: no good because B2 remains of type base
//    B2=new;
//    B2.copy(B1);
///// option 3: works
    void'($cast(B2, B1.clone()));
///// end options                                    
    $display("B2 type is %s", B2.get_type_name());