Casting from parent class to child class

In reply to chr_sue:

Hi ,

Yes I am doing strange things indeed :-) but reason was because casting was not working as expected in normal way.

Both derived class(MY Test Bench) and parent class(UVC) in my env are registered in factory.
I have factory override of base class with derived class.
In my TB I am populating the fields of base class properties
base.a = x;
base.b = y;
base.c = z;

And now under some condition I need to populate the properties of derived class as well.
child.d = xyz;

I can not do base.d = xyz as I will get compile error.

so I need to cast back base class to derived class before I do child.d = xyz

I created base class object using create method
Ideally for my case $cast(child,base) should work as my factory override would point create call of base object to child class type.

However somehow that was not working in my env. May I should stop here itself and first debug that before moving ahead and using workarounds.

Anyways I found a workaround. I created an object of child class child_obj_2 and then copy this to base class object base_obj_1 and then do the casting, so that my base class object explicitly points to child class type when casting was executed.

I finally write base class object to write port and give it to UVC sequence to process.

In UVC sequence I have corresponding get call . transaction type on analysis port/export is base class type.

After get call I need to access the newly added properties of derived class from UVC sequence. Now to be able to that I again need to cast base class object that I have received through get call to the child class handle . This is not working.

I accept that my example is not illustrative , but code is spread across many components , will try to create small representative test as Dave suggested.

Regards