Hi SystemVerilog forum.
I am trying to use polymorphism to make the code easier.
The following code works but I don’t want to use it
interface class inter;
pure virtual function void print();
endclass: inter
class private_name implements inter;
virtual function void print;
$display("Shimon");
endfunction: print
endclass: private_name
class last_name implements inter;
virtual function void print;
$display("Cohen");
endfunction: print
endclass: last_name
class name;
private_name pname;
last_name lname;
function void print_person();
pname = new();
lname = new();
pname.print();
lname.print();
endfunction: print_person
endclass
module top;
name m_name = new();
initial begin
m_name.print_person();
end
endmodule
In the following code I placed private_name and last name in an array of type inter.
Obviously it won’t work because inter cant be instantiated. how can I $cast this structure?
I need this structure because In my project I going to add many classes that implements inter, and all I want to do is to
add the name of the new class in the array.
class name;
inter m_inter[]={private_name, last_name};
function void print_person();
foreach(m_inter[i]) begin
m_inter[i] = new();
m_inter[i].print();
end
endfunction: print_person
endclass