Method/function overloading

We have method overriding in system verilog, where the method in derived class can override the base class’s method. Both the methods should be of same name and same signature.

Do we have method overloading in system verilog? I mean does SV supports this concept?

If it is supported, can any body tell me the context in testbench or test development?

Thanks
Rajeshwar

Yes, this is a very basic principal of a class based testbench. Search the internet for “SystemVerilog virtual method” for some examples.

1 Like

AFAIK “overloading” is not available in SV (unlike in VHDL for instance). Maybe if you explain what you need, we can help you find a way to do it in SV.

Ajeetha, CVC

In reply to ajeetha:

Overriding/overloading a method with the same signature can be the same as defining a virtual method. The term method overloading usually applies to methods with a different signature (i.e. different types or numbers of arguments).

I agree with Dave. Function overloading normally refers to the case where you have the same function name with different signatures.

A simple example in C++;

class C {
virtual void print(int x);
virtual void print(float y);
}

Note the same function “print” has two different signatures. This is a very useful feature. However this is not supported in
SystemVerilog as far as I know. Not sure if there are plans in the committee to support this in the future.

Thanks

Logie.

In reply to logie:

Thank you all.

@Dave, I understand the method/function overriding in System verilog. I was looking for the scenario like Logie explained, where we have the two methods with same name but different signatures in SystemVerilog. So it is not supported for sure in systemverilog.

Thanks,
Rajeshwar

In reply to RAJESHWAR:

OK. You originally wrote

Both the methods should be of same name and same signature.

AFAIK, there are no plans to add this to SystemVerilog.

In reply to dave_59:

Thanks Dave.

“We have method overriding in system verilog, where the method in derived class can override the base class’s method. Both the methods should be of same name and same signature”

Quote:
Both the methods should be of same name and same signature.

Actually, I mentioned the above thing continuing the context of method overriding.

Any way, I am sorry for the confusion created.

Thank you.

Rajeshwar

In reply to RAJESHWAR:

Static methods can be overloaded, that means a class can have more than one static method of same name. But static methods cannot be overridden, even if you declare a same static method in derived class it has nothing to do with the same method of base class…Source

Dov

In reply to ephraimdov:

Dov,

This is not true in SystemVerilog - there is no method overloading .

Dave

In reply to ephraimdov:

Even static method does not support overloading.

I’m reading the documentation of :

static function uvm_object_wrapper uvm_object::get_type ();

And in the function documentation, the example suggests:

class cmd extends uvm_object;
  typedef uvm_object_registry #(cmd) type_id;
    static function type_id get_type();
       return type_id::get();
    endfunction
  endclass

In the example, the return type is type_id and it is different than uvm_object_wrapper, how is this overloading work even if we have two functions with the same name but different signatures?

Thanks,
Tony

Hi Tony,
I’m not sure if this example can be classified as overloading, because the type_id is a uvm_object_registry which, in turn, is derived from uvm_object_wrapper. So, because of that type_id is “accepted” as a uvm_object_wrapper as well.
But if there was no inheritance in this case, this example would not work in SystemVerilog.

1 Like

The LRM section 8.20 Virtual methods specifically allows

The return type of a virtual function shall be either:
— a matching type (see 6.22.1)
— or a derived class type

This is known as covariance in OOP terminology, not overloading.

2 Likes

Hi Dave,

Please let me take this opportunity to ask a more general OOP question, so.
Both covariance and overloading are types of polymorphism, right?
And in the following pseudo code example:

class animal;
class human extends animal;
animal animal_obj;
animal_obj = human.new();

Is it called covariance too? Or just polymorphism?

Best regards,
Jorgeluis.

That is a typed constructor, all examples of polymorphism.
Section 8.8 Typed constructor calls in the IEEE 1800-2023 SystemVerilog LRM