1

My question is regarding using $cast in SV. If you search for the word cast in the code below, I have on purpose added a '!' to check for unsuccessful casting. In the event of unsuccessful cast, I wanted to see what happens when a call is made to bobsquare.bob(); I am surprised that at time=1ms when call_bob is called with the handle to polygon 'p', the function 'bob' in square class is called to perform the display statements. How can that be possible? I ran with Cadence's irun and debugged with the SV class browser and see that at time=1ms, bobsquare is not even allocated mem space and has NULL as pointer. Thanks!

class figure;

endclass

class polygon extends figure;

  virtual function void draw();
    $display("polygon::draw");
  endfunction

endclass

class square extends polygon;

  virtual function void draw();
    $display("square::draw");
  endfunction

  function void compute_area();
    $display("square::compute_area");
  endfunction

  function void bob();
     $display("square::I am bob");
     $display("%t", $realtime);     
  endfunction

endclass

program top;
  polygon p;
  square s;

  initial begin
    s = new();
    p = new();

     #1ms;
     call_bob(p);
     #1ms;
     call_bob(s);     
  end // initial begin
   task call_bob(figure generic_ref_figure);
      square bobsquare;      
      if (!($cast(bobsquare, generic_ref_figure)))
         bobsquare.bob();
   endtask // call_bob

endprogram
4

1 回答 1

0

IEEE Std 1800-2012 § 8.4(& IEEE Std 1800-2005 § 7.4)规定:

通过空对象句柄访问非静态成员(参见 8.9)或虚拟方法(参见 8.20)是非法的。通过空对象进行非法访问的结果是不确定的,实现可能会发出错误。

我找不到任何关于默认方法类型应该发生什么的参考。基于 LRM,只要方法不调用任何非静态成员,通过空对象句柄调用非虚拟方法似乎是合法的。

使方法 bobvirtual获取空对象句柄错误。

于 2013-07-24T16:59:53.457 回答