Overloading Functions¶
CrossBasic allows multiple methods (or functions) with the same name but different signatures, enabling polymorphic behavior.
Method Overloading¶
Within a class, define multiple Subs/Functions sharing a name but differing in parameter lists:
Class Pair
Sub Constructor() End Sub
Sub Constructor(left As Variant, right As Variant)
Self.LeftValue = left
Self.RightValue = right
End Sub
End Class
The compiler picks the overload by matching argument count and types .
Global Function Overloading¶
Outside classes, you can also overload standalone functions:
Function Add(a As Integer, b As Integer) As Integer
Return a + b
End Function
Function Add(a As Double, b As Double) As Double
Return a + b
End Function
Print(Add(2, 3)) // calls first overload
Print(Add(2.5, 3.1)) // calls second overload
- Return type is not part of the signature—overloads must differ by parameter count or types.
- The call site determines which overload is used.