Cast couldn't get variable from clone()

I’m trying to understand the usage of $cast() within clone() from Class Variables and $cast - Verification Horizons

So I made a simple example as the following.


class seq_item extends uvm_sequence_item;
  `uvm_object_utils(seq_item)

  randc bit[15:0] addr;
  randc bit[15:0] data;


....
endclass

class scoreboard_c extends uvm_scoreboard;                                                    
  `uvm_component_utils(scoreboard_c)                                                          
  `uvm_analysis_imp_decl(_my)                                                                 
  uvm_analysis_imp_my #(seq_item, scoreboard_c) sb_my_in;                                     
  seq_item item_q[$];                                                                         
                                                                                              
  function new (string name = "scoreboard_c", uvm_component parent);                          
    super.new(name, parent);                                                                  
    sb_my_in = new("sb_my_in", this);                                                         
  endfunction                                                                                 
                                                                                              
  function void build_phase(uvm_phase phase);                                                 
    super.build_phase(phase);                                                                 
  endfunction                                                                                 
                                                                                              
  virtual function void write_my(seq_item packet);                                            
  seq_item sb_packet;                                                                         
                                                                                              
  `uvm_info(get_type_name, $sformatf("packet data=%d, addr=%d", packet.data, packet.addr), UVM_LOW)
                                                                                              
  if(!$cast(sb_packet, packet.clone()))                                                       
   $error("Cast fail");                                                                       
                                                                                              
  `uvm_info(get_type_name, $sformatf("sb_packet data=%d, addr=%d", sb_packet.data, sb_packet.addr), UVM_LOW)
                                                                                              
  item_q.push_back(sb_packet);                                                                
  endfunction                                                                         
                                                                                      
                                                                                              
endclass

There is no fail of $cast, But I get all ‘0’ cloned value

UVM_INFO scoreboard_c.sv(21) @ 2850: uvm_test_top.env_o.scoreboard [scoreboard_c] packet data=    2, addr=    3
UVM_INFO scoreboard_c.sv(26) @ 2850: uvm_test_top.env_o.scoreboard [scoreboard_c] sb_packet data=    0, addr=    0
...

How do I correctly $cast from clone()?

In reply to UVM_LOVE:

Did you implement do_copy()?

In reply to dave_59:

I get some clues, sometimes we want to modify a local copy of the object, so we clone it (construct+copy=clone) and $cast is needed when using clone() with derived types.
The copy method is not virtual and should not be overloaded in derived classes. To copy the fields of a derived class, that class should override the do_copy method.