Operator Procptr (Procedure Pointer)
 
Returns the address of a procedure

Syntax

Declare Operator ProcPtr ( ByRef identifier As proctype [, proctype ] ) As proctype Ptr

Usage

result = ProcPtr ( identifier [, proctype ] )

Parameters

identifier
A procedure identifier.
proctype
Any type of procedure (sub/function).

Return Value

Returns the address of the procedure.

Description

This operator returns the address of a Sub or Function procedure.

When using the two arguments PROCPTR( identifier, type ) syntax, this allows of getting procedure pointer for based on sub/function type.
This makes it possible to explicitly specify the 'type' of the sub/function, to resolve procedure overloads or make a check for compatible sub/function on non-overloaded procedures.

Operator @ (Address Of), when used with procedures, behaves the same as ProcPtr without its optional argument (the second).

Example

' This example uses ProcPtr to demonstrate function pointers
Declare Function Subtract( x As Integer, y As Integer) As Integer
Declare Function Add( x As Integer, y As Integer) As Integer
Dim myFunction As Function( x As Integer, y As Integer) As Integer

' myFunction will now be assigned to Add
myFunction = ProcPtr( Add )
Print myFunction(2, 3)

' myFunction will now be assigned to Subtract.  Notice the different output.
myFunction = ProcPtr( Subtract )
Print myFunction(2, 3)

Function Add( x As Integer, y As Integer) As Integer
    Return x + y
End Function

Function Subtract( x As Integer, y As Integer) As Integer
    Return x - y
End Function

Sub s Overload()
End Sub

Sub s( ByVal i As Integer )
End Sub

'----- since fbc 1.09.0, ProcPtr supports a second parameter (optional):
Var s1 = ProcPtr( s, Sub() )
Var s2 = ProcPtr( s, Sub( ByVal i As Integer ) )

'----- before fbc 1.09.0, it was only possible with:
'Dim s1 As Sub()
's1 = ProcPtr( s )
'Dim s2 As Sub( Byval i As Integer)
's2 = ProcPtr( s )

Version

  • Before fbc 1.09.0, the second argument (the optional) was not supported.

Dialect Differences

  • Not available in the -lang qb dialect unless referenced with the alias __Procptr.

Differences from QB

  • New to FreeBASIC

See also