0
我試圖把握鑄造的SystemVerilog中的概念,並已用下面的代碼修修補補:
class packet;
virtual function int compute_crc();
compute_crc = 12345;
endfunction
virtual task print;
$display("This is a packet");
endtask
endclass: packet
class bad_packet extends packet;
function int compute_crc();
compute_crc = 54321;
endfunction
task print;
$display("This is a bad packet");
endtask
task print2;
$display("This is not accessible from base");
endtask
endclass: bad_packet
module test;
packet pkt;
bad_packet b_pkt;
initial begin
b_pkt = new();
pkt = b_pkt;
$cast(b_pkt, pkt);
b_pkt.print;
pkt.print;
end
endmodule
我有一個基類「包」和派生類的bad_packet「。通過使用$ cast,我可以訪問bad_packet的方法print2嗎?還有其他的方法嗎?謝謝!
我能夠從數據包類訪問bad_packet的compute_crc方法,但我試圖從數據包類內部訪問bad_packet的print2方法,因爲該數據包內不存在該方法,所以標記了一個錯誤。那麼這是否意味着只有在基類中存在該方法的虛擬版本時,我才能從擴展類訪問方法? –
如果將方法定義爲**基類類型**中的虛方法,則只能訪問其**句柄存儲在**基類變量**中的**擴展類對象**中的方法。 –