Get interface type inside a class and declare virtual interface

Can I do this in a class ?

class class_name#(type INTF);
virtual INTF if;

endclass

I get the error:
Error-[WUCIQ] Invalid qualifier usage
Invalid use of class item qualifiers. Cannot use virtual keyword for
variables.

The following code executes perfectly fine

interface axi_intf;
  logic clk , reset_n;
endinterface  

class C#(type VINTF = virtual interface axi_intf);
  VINTF intf;

  function new();
    $display("Within class C");
  endfunction  
endclass  
  
module tb();
  
  axi_intf axi_intf_inst();
  
  C#(virtual interface axi_intf) c1 = new();
  
endmodule  
1 Like

Thanks for your response. Why are the below implementations not supported ?

class class_name#(type INTF);
virtual INTF if;

(or)

class class_name#(interface INTF);
virtual INTF if;

I intend to create a customizable class by passing the interface types upon creation.
With the solution you provided, I’m able to instantiate the virtual interface. However, to get the interface I also need the interface type to be passed.

An interface is not a datatype–it is a design element like a module. A virtual interface is a datatype. You can pass the virtual interface datatype as a type parameter to a class.

1 Like

@Llama
One point I would like to add is that the keyword ‘interface’ is optional in the type declaration.
We could also define the class as

class C#(type VINTF = virtual axi_intf); // Without keyword 'interface'
  VINTF intf;

  function new();
    $display("Within class C");
  endfunction  
endclass