How to call child class function having only parent class object

Dear Forum,

I have only parent class object, how can I access to its child class function.

This is my code:

class trans extends uvm_transaction;
...
endclass

class mytrans extends trans;

function myPrint (..);
...
endfunction

endclass
// **********************

class Test extends uvm_test;

myCallback cb;
trans t1;

task run_phase (uvm_phase phase);
 cb.monitorTraffic(t1);
 
endtask

endclass


class myCallback extends intCallBackMon;

function monitorTraffic( trans t1);

mytrans it1;

$cast (it1, t1);  // This gives ERROR
it1.myPrint();  // I need to call myPrint function for t1 object

endfunction

endclass

I need here to call my own myPrint function for trans object.
Can someone please help understand how I can do it?

Thanks
Hayk

In reply to haykp:

With this code, the cast will never work and you will not able to access myPrint method from current transaction. The it1 and t1 are totaly different object handlers.

The cast only works in your code only when you declare t1 handle with type myTrans in Test class:


class Test extends uvm_test;
...
myTrans t1;
...
endclass

Can you explain in more detail what is your purpose in your case.
Also please review again OOP especially Polymorphism in SV

In reply to haykp:

You need to provide a more complete example, and show us the error you are getting. You also should be testing the return value of $cast.

In reply to cuonghle:

Thanks for the answer.
I think my example is not good one because of that you got confused.

So we have a VIP from vendor, in that VIP there is a predefined callbacks with their methods.
When some event happens in the system, than that callback triggers a function. That function proto is following:

 virtual function void CompletionQueueEnterCbF (vendorTrans trans);

now vendorTrans is coming together with the VIP, but I want to change that to myTrans type.
I cannot edit the CompletionQueueEnterCbF function definition as it is VIP files.

I tried to mimic this scenario using the example. In that example assume that

function monitorTraffic( trans t1);

is vendor function

In reply to haykp:

What your intention in your mytrans which extended from vendor trans?
We don’t know how the VIPs is implemented, then I cannot give you the good recommendations.
But can you try the following ways:
I assume mytrans is the class which extended from vendor trans.

  1. Create mytrans in your sequence and start_item/finish_item with mytrans, then in the callback function, you cast the vendor trans to mytrans.

  2. Override (by type) mytrans class to vendor class, then cast the vendor trans to mytrans in the callback. Please check to make sure that you are using type_id::create method for the vendor trans in your sequence.

  3. Try to clone/copy the vendor trans to mytrans in your callback.

I hope case #1 or case #2 will work, but it depends on how your VIP handles the trans before calling the callback function.

In reply to cuonghle:

Dear Chris Le,

What your intention in your mytrans which extended from vendor trans?
I want to extend from vendor trans, and add more customization in it for example do many printing, or do packet decoding for easy human reading.

Imagine there is a VIP callback, which always needs trans object as a input.
But I want to replace that trans object with my own mytans object.
I cannot do casting, as callback function always calls trans.

your 3 suggestions I think will resolve my issue. I will try at first 2), than 3).
Could not clearly understand 1) suggestion.

thanks for your help.