Difference Between Virtual and Pure Virtual

Hi All,

Can any body tell me the difference between

Virtual and Pure Virtual Function in System Verilog.

Hi,

They have the same meaning as in C++. A virtual function can be overridden in a derived class. If a object of that derived class is accessed using a handle to its base class, the function call is performed polymorphically - the function called is determined by the type of the object pointed to, not the type of the handle (so the overridden function is called if the object pointed to is of the derived class type).

A pure virtual function is a member of an “abstract” base class. You cannot create an object of an abstract class. No implementation need be provided for the pure virtual function in the base class but it must be overridden in a derived class if you want to create objects of that type. Pure virtual functions are used to create “interface” classes (not to be confused with the SystemVerilog interface structure). You find examples of these in the OVM TLM classes where they are used to define the set of interface methods required by ports and provided by exports.

Hope that helps your understanding.

Regards,
Dave

In reply to kunal_1514:

Virtual function is a function template in your base class that may be optionally overriden in your derived class with new code.

Virtual pure function is a function template in your base class that MUST BE overriden in your derived class with new code.

This difference is useful if you want to force the user of your code to complete or provide addional data that is needed for operation. This is done with this virtual pure functions. This act as hook where the user MUST include the implementation needed and if he forget it, then it will be a compile error. However, with virtural functions the user may or may not overwrite those functions.
e.g.


virtual class BasePacket;
  pure virtual function integer send(bit[31:0] data); // No implementation
endclass
class EtherPacket extends BasePacket;
  virtual function integer send(bit[31:0] data);
    // body of the function
    ...
  endfunction
endclass

BR
Jonathan