ZetCode

FreeBasic Operator Keyword

last modified June 16, 2025

The FreeBasic Operator keyword enables operator overloading for user-defined types. It allows custom behavior for standard operators when working with custom types.

Basic Definition

Operator overloading lets you define how operators behave with custom types. FreeBasic supports overloading most operators including arithmetic, comparison, and assignment operators.

The Operator keyword is used to create operator procedures that specify the behavior when the operator is used with custom types. This makes code more intuitive and readable.

Overloading the + Operator

This example shows how to overload the addition operator for a custom vector type.

operator_add.bas
Type Vector2D
    x As Single
    y As Single
    
    Declare Operator Cast() As String
End Type

Operator + (a As Vector2D, b As Vector2D) As Vector2D
    Dim result As Vector2D
    result.x = a.x + b.x
    result.y = a.y + b.y
    Return result
End Operator

Operator Vector2D.Cast() As String
    Return "(" & Str(x) & ", " & Str(y) & ")"
End Operator

Dim v1 As Vector2D = (1.5, 2.5)
Dim v2 As Vector2D = (3.5, 4.5)

Dim sum As Vector2D = v1 + v2

Print "v1: "; v1
Print "v2: "; v2
Print "sum: "; sum

Here we define a Vector2D type and overload the + operator to add two vectors component-wise. We also define a cast operator to convert the vector to a string for printing. The operator makes vector addition look natural.

Overloading the * Operator

This example demonstrates overloading the multiplication operator for scalar multiplication of vectors.

operator_multiply.bas
Type Vector2D
    x As Single
    y As Single
End Type

Operator * (v As Vector2D, scalar As Single) As Vector2D
    Dim result As Vector2D
    result.x = v.x * scalar
    result.y = v.y * scalar
    Return result
End Operator

Operator * (scalar As Single, v As Vector2D) As Vector2D
    Return v * scalar
End Operator

Dim vec As Vector2D = (2.0, 3.0)
Dim scaled1 As Vector2D = vec * 1.5
Dim scaled2 As Vector2D = 2.0 * vec

Print "Original: ("; vec.x; ", "; vec.y; ")"
Print "Scaled 1: ("; scaled1.x; ", "; scaled1.y; ")"
Print "Scaled 2: ("; scaled2.x; ", "; scaled2.y; ")"

We overload the * operator twice to handle both vector * scalar and scalar * vector cases. The second overload calls the first to avoid code duplication. This makes vector scaling operations more intuitive.

Overloading Comparison Operators

This example shows how to overload comparison operators for a custom type.

operator_compare.bas
Type Book
    title As String
    pages As Integer
    
    Declare Operator = (b As Book) As Boolean
    Declare Operator < (b As Book) As Boolean
End Type

Operator Book.= (b As Book) As Boolean
    Return title = b.title And pages = b.pages
End Operator

Operator Book.< (b As Book) As Boolean
    Return pages < b.pages
End Operator

Dim book1 As Book = ("FreeBasic Guide", 350)
Dim book2 As Book = ("Advanced FB", 500)
Dim book3 As Book = ("FreeBasic Guide", 350)

Print "book1 = book2: "; (book1 = book2)
Print "book1 = book3: "; (book1 = book3)
Print "book1 < book2: "; (book1 < book2)

We define equality and less-than operators for the Book type. Two books are equal if both title and page count match. One book is less than another if it has fewer pages. These operators enable natural comparison syntax.

Overloading the Cast Operator

This example demonstrates overloading the cast operator for type conversion.

operator_cast.bas
Type Temperature
    celsius As Single
    
    Declare Operator Cast() As Single
    Declare Operator Cast() As String
End Type

Operator Temperature.Cast() As Single
    Return celsius
End Operator

Operator Temperature.Cast() As String
    Return Str(celsius) & "°C"
End Operator

Dim temp As Temperature = (25.5)
Dim fahrenheit As Single = temp * 9 / 5 + 32

Print "Temperature: "; temp
Print "In Fahrenheit: "; fahrenheit

We overload the cast operator twice to convert a Temperature to both Single and String. The numeric cast enables arithmetic operations, while the string cast provides a formatted representation. This makes the type more versatile.

Overloading the Assignment Operator

This example shows how to overload the assignment operator (=).

operator_assign.bas
Type StringWrapper
    value As String
    
    Declare Operator Let (s As String)
    Declare Operator Cast() As String
End Type

Operator StringWrapper.Let (s As String)
    value = "Wrapper: " & s
End Operator

Operator StringWrapper.Cast() As String
    Return value
End Operator

Dim wrapper As StringWrapper
wrapper = "Test String"

Print wrapper

We overload the assignment operator (Let) to modify how strings are assigned to our StringWrapper type. The operator adds a prefix to the assigned string. This demonstrates custom assignment behavior.

Overloading the Index Operator

This example demonstrates overloading the array index operator ([]).

operator_index.bas
Type IntArray
    Private:
    Dim data(0 To 9) As Integer
    
    Public:
    Declare Operator [] (index As Integer) ByRef As Integer
End Type

Operator IntArray.[] (index As Integer) ByRef As Integer
    If index < 0 Or index > 9 Then
        Print "Index out of bounds"
        Return data(0)
    End If
    Return data(index)
End Operator

Dim arr As IntArray
arr[3] = 42
arr[10] = 99 ' Will trigger error message

Print "arr[3] = "; arr[3]

We overload the index operator to provide bounds checking for our custom array type. The operator returns a reference to allow modification of array elements. Invalid indexes trigger an error message and return the first element.

Overloading the Function Call Operator

This example shows how to overload the function call operator (()).

operator_call.bas
Type Multiplier
    factor As Integer
    
    Declare Operator () (x As Integer) As Integer
End Type

Operator Multiplier.() (x As Integer) As Integer
    Return x * factor
End Operator

Dim double As Multiplier = (2)
Dim triple As Multiplier = (3)

Print "Double 5: "; double(5)
Print "Triple 5: "; triple(5)

We create a Multiplier type that multiplies numbers by a fixed factor. Overloading the function call operator lets us use instances like functions. This creates flexible, configurable function-like objects.

Best Practices

This tutorial covered the FreeBasic Operator keyword with practical examples showing operator overloading in different scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all FreeBasic Tutorials.