Confused between $cast and factory override

dear all,

I’m trying to understand difference between $cast and override.

And I came across good example as the below

module test_module ();

`include "uvm_macros.svh"
  import uvm_pkg::*;

  class agent_a extends uvm_agent;
    `uvm_component_utils(agent_a)
  
    function new(string name = "agent_a", uvm_component parent = null);
      super.new(name, parent);
    endfunction : new
  endclass : agent_a
  
  class agent_b extends agent_a;
    `uvm_component_utils(agent_b)
    
    string field = "field";
  
    function new(string name = "agent_b", uvm_component parent = null);
      super.new(name, parent);
    endfunction : new
  endclass : agent_b

  agent_a agent_a_h;
  uvm_agent temp;
  agent_b agent_b_h;
  agent_a agent_a_h_2;
  int cast1, cast2, cast3;

  initial begin
    agent_a::type_id::set_type_override(agent_b::get_type());
    factory.print();
    agent_a_h = agent_a::type_id::create("agentti_h", null);
    temp = agent_a_h;
    cast1 = $cast(agent_a_h_2, agent_a_h); // This $cast works also with type override, why?
    cast2 = $cast(agent_b_h, agent_a_h);  // This $cast won't work without type override, why?
    cast3 = $cast(agent_b_h, temp);  // This $cast won't work without type override either.
    $display("casts = ", cast1, cast2, cast3);
    $display("field = ", agent_b_h.field);
  end
endmodule : test_module

As I understand, step by step
1.requested type agent_a and override type agent_b. so agent_a is agent_b.
2.agent_a_h is created by agent_b not agent_a because type override.
so we can assume that agent_a agent_a_h is agent_b agent_a_h and agent_a agent_a_h_2 is agent_b agent_a_h_2

  1. cast1 is the same as agent_b class’s agent_a_h_2 and agent_b class’s agent_a_h.
    so they are same object type. no problem.

  2. cast2 is the same as agent_b class’s agent_b_h and agent_b class’s agent_a_h.
    so they are same object type. no problem

  3. This cast3 didn’t get it. how does cast between agent_b and uvm_agent?
    When I ran the Simulation, There is no Error.
    And What is the point the difference between cast and override? I couldn’t find it.

https://www.edaplayground.com/x/Gpce
Could you guide me?

In reply to UVM_LOVE:

2.agent_a_h is created by agent_b not agent_a because type override.
after doing type override agent_a with agent_b, then while creating agtent_a object using create method actually it creates object for agent_b and returns handle of agent_b (note: this handled by UVM DB and factory override)
3. cast1 is the same as agent_b class’s agent_a_h_2 and agent_b class’s agent_a_h.
so they are same object type. no problem.
Yes…

cast2 = $cast(agent_b_h, agent_a_h); // This $cast won’t work without type override, why?
it won’t work because without override, agent_a_h variable contains the class handle(i.e. object instance memory location) for agent_a, so…agent_b_h and agent_a_h are different class types and hence it won’t work

In reply to Desam:

In reply to UVM_LOVE:
2.agent_a_h is created by agent_b not agent_a because type override.
after doing type override agent_a with agent_b, then while creating agtent_a object using create method actually it creates object for agent_b and returns handle of agent_b (note: this handled by UVM DB and factory override)

  1. cast1 is the same as agent_b class’s agent_a_h_2 and agent_b class’s agent_a_h.
    so they are same object type. no problem.
    Yes…
    cast2 = $cast(agent_b_h, agent_a_h); // This $cast won’t work without type override, why?
    it won’t work because without override, agent_a_h variable contains the class handle(i.e. object instance memory location) for agent_a, so…agent_b_h and agent_a_h are different class types and hence it won’t work

Thanks reply,
But If you can see the result in my EDAPLAYFROUND link, cast1, cast2, cast3 are all 1.
Meaning that All casts are working as well, I don’t understand especially cast2 and cast3.

In reply to UVM_LOVE:

I did see below results from eda playground:
with type override: agent_a::type_id::set_type_override(agent_b::get_type()); //enabling

casts = 1 1 1

without type override: // agent_a::type_id::set_type_override(agent_b::get_type()); //commented it out

casts = 1 0 0

i did see expected results here…

In reply to Desam:

In reply to UVM_LOVE:
I did see below results from eda playground:
with type override: agent_a::type_id::set_type_override(agent_b::get_type()); //enabling

casts = 1 1 1

without type override: // agent_a::type_id::set_type_override(agent_b::get_type()); //commented it out

casts = 1 0 0

i did see expected results here…

Yes. I got the same result.
Could you let me know Handle type and Object Type from the below?

agent_a agent_a_h;
uvm_agent temp;
agent_b agent_b_h;
agent_a agent_a_h_2;

I think there is no Objection Type.

Handle Types are

agent_a ;
uvm_agent ;
agent_b ;
agent_a ;

Am I right?

In reply to UVM_LOVE:

Handle Types are

agent_a ;
uvm_agent ;
agent_b ;
agent_a ;

Am I right?

no, these are class Types which contains set of members and set of methods that operate on those members

agent_a_h,agent_b_h,temp : these are class variables where you store the class handles that references a particular class object of a particular class type
more info: A Short Class on SystemVerilog Classes - Verification Horizons

In reply to Desam:

In reply to UVM_LOVE:
Handle Types are
agent_a ;
uvm_agent ;
agent_b ;
agent_a ;
Am I right?
no, these are class Types which contains set of members and set of methods that operate on those members
agent_a_h,agent_b_h,temp : these are class variables where you store the class handles that references a particular class object of a particular class type
more info: A Short Class on SystemVerilog Classes - Verification Horizons

I see that A Short Class on SystemVerilog Classes - Verification Horizons
But I couldn’t find what I want about Type Object and Handle Object.
Could you give me some example for understanding ?

In reply to UVM_LOVE:

The answer is very simple:
The 'override is limited to the UVM. SV does not have a override command.
The $cast is a so-caled dynamic type cast. You can use the $cast independently from the UVM and it is doing a type cast to types (classes) which are belonging to the same family.

In reply to UVM_LOVE:

In reply to Desam:
I see that A Short Class on SystemVerilog Classes - Verification Horizons
But I couldn’t find what I want about Type Object and Handle Object.
Could you give me some example for understanding ?

Hi,
What is your doubt? cast2 or cast3 or both ??