SystemVerilog将派生的类句柄分配给基类对象
这似乎是我应该可以做的简单的事情,但我想我不完全理解多态性。我从监视器的分析端口获得了一个基类对象,并希望从此基类对象创建一个派生的类对象,然后设置派生类的额外成员(用于覆盖范围)。这样做的最简单方法是什么?
class BasePkt extends uvm_object;
//...
endclass
.
class CovPkt extends BasePkt;
//more members...
endclass
.
task process_pkt();
forever begin
BasePkt base_pkt;
CovPkt cov_pkt;
pkt_ap.get(base_pkt);
cov_pkt = base_pkt; // Compile error
end
endtask
我可以创建一个在COVPKT对象中执行basepkt的深层副本的函数,但这似乎不是正确的事情。由于COVPKT包含Basepkt的所有成员,因此我为什么不能将其分配给Basepkt类,然后让不在Basepkt中的CoVPKT成员成为其默认值?
This seems like a simple thing I should be able to do but I guess I don't fully understand polymorphism. I get a base class object from a monitor's analysis port and want to create a derived class object from this base class object, and then set extra members of the derived class (for coverage). What's the simplest way to go about doing this?
class BasePkt extends uvm_object;
//...
endclass
.
class CovPkt extends BasePkt;
//more members...
endclass
.
task process_pkt();
forever begin
BasePkt base_pkt;
CovPkt cov_pkt;
pkt_ap.get(base_pkt);
cov_pkt = base_pkt; // Compile error
end
endtask
I could create a function that performs a deep copy of BasePkt into a CovPkt object, but that doesn't seem like the right thing to do. Since CovPkt contains all the members of BasePkt, why can't I just assign it to a BasePkt class and then let the members of CovPkt that aren't in BasePkt be their default value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为你不能。
当您从一个类变量进行分配到另一个类变量时,您将复制一个类句柄,而不是类对象本身。句柄是对构造对象的参考。在您的示例中,我假定
base_pkt
包含basepkt
class Object的句柄。该对象仅包含保持basepkt
成员的空间。如果允许您直接将basepkt
对象分配到covpkt
类变量,则引用covpkt
成员将在该空间之外。您需要首先构造
covpkt
类对象,然后复制base_pkt
成员是正确的。但是,由于您正在使用UVM,因此一种更好的方法是通过使用工厂在监视器中的源中简单地创建covpkt
。您可以使用covpkt
覆盖basepkt
,而无需更改监视器中的任何代码。然后,您将能够在任务中执行动态铸件。您可能想在。
Because you can't.
When you make an assignment from one class variable to another, you are copying a class handle, not the class object itself. A handle is a reference to a constructed object. In your example, I presume
base_pkt
contains a handle to aBasePkt
class object. The object only contains the space to hold theBasePkt
members. If you were allowed to directly make an assignment of aBasePkt
object to aCovPkt
class variable, references toCovPkt
members would be outside that space.You were correct in needing to construct a
CovPkt
class object first, and then copying thebase_pkt
members. However, since you are using the UVM, a better approach is to simply create aCovPkt
at the source in the Monitor by using the factory. You can overrideBasePkt
withCovPkt
without changing any code in the Monitor. Then you will be able to perform a dynamic cast in your task.You might want to take a look at my course on SystemVerilog OOP for UVM.