Saturday, July 15, 2017

conc_cast_dynamic_up_vs_down

class Base;
endclass

class Exten extends Base;
endclass

program main;
  initial begin
    Base B;
    Exten E;
    B = new();
    //##E = new();          //##DEBUG: MASTER: doing this will also not allow us to down cast
    if(!$cast(E,B)) begin   //##DEBUG: MASTER: NOTE: as this unsuccessful cast will return '0' value
      $display("\n");
      $display(" Base class object B can'T be assigned to Extended class Handle.");
      $display(" So this implies 'down casting is not possible'\n");
    end
    // Deallocate object B
    //##DEBUG: MASTER: NOTICE: even if we do not include below null assignment, result will be same
    B = null;
    E = new();
    if($cast(B,E)) begin    //##DEBUG: MASTER: NOTE: as this successful cast will return '1' value
      $display(" Extended class object E can be assigned to Base class Handle.");
      $display(" So this implies 'up casting is only possible'\n");
    end
  end
endprogram

//##DEBUG: MASTER: run results
/*
ncsim> run

 Base class object B can'T be assigned to Extended class Handle.
 So this implies 'down casting is not possible'

 Extended class object E can be assigned to Base class Handle.
 So this implies 'up casting is only possible'

Simulation complete via implicit call to $finish(1) at time 0 FS + 1
*/

No comments:

Post a Comment