Hi all,
according to my understanding of OOP in general I can always pass an extended class handle to a base class handle since clearly the base class (and the objects that operates on it) knows perfectly which members and methods are available. But the contrary is not true, since the base class cannot possibly know how to manipulate additional members/methods that have been defined in the extended class.
Yet I have an uvm_object instantiated in a component and now I’m extending the component and would like to also use an extended class of the original uvn_object. So this is my simplified use case:
class base_object extends uvm_object;
// utils and other stuff omitted
int a;
endclass
class extended_object extends base_object;
int b;
endclass
class base_component extends uvm_component;
// utils and other stuff omitted
base_object obj;
virtual function do_something();
add_one_to_a();
add_one_to_b();
endfunction
virtual function void add_one_to_a ();
obj.a++;
endfunction
// this class needs to be extended by the extended_component since b is not a member of obj
virtual function void add_one_to_b ();
endfunction
endclass
class extended_component extends base_component;
// utils and other stuff omitted
extended_object e_obj;
function new (string name="")
e_obj = extended_object::type_id::create("extended_obj");
endfunction
virtual function void add_one_to_b ();
// here I override the base_component method so that I can operate on my extended class
e_obj.b++;
endfunction
function void run();
// In order to do something with e_obj, I would need to pass the e_obj
// handle to the obj in such a way that when the "do_something" method
// is called I would execute the add_one_to_a from the base_component
// and add_one_to_b from the extended component on the extended object
// obj = e_obj // not working
// _________ <----- fill the gap!
do_something();
endfunction
endclass
At this point I’ve tried to pass the e_obj handle to the obj handle, but I get a nasty “assignment operator type check failed”
obj = e_obj; // <-- type check failed
Which doesn’t seem clear to me since assigning an extended class to a base class is the foundation of polymorphism and what makes possible to reconfigure an entire uvm environment (through factory and overrides).
Is what I’m trying to do fundamentally wrong? In practice my usecase is a little more elaborated and the component at hand is a subscriber whose ‘write’ method is operating on a base object through a method that is actually defined in the extended subscriber.
I realize it might be a little confusing, so do not hesitate to ask question so that I can clarify my question as well.
Any pointer/recommendation is appreciated.
[1] here’s an example of polymorphism: class polymorphism - EDA Playground