Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
n00b question...
#1
i was wondering can someone tell me what a function pointer is/ does x.x i never heard of it until fb
Reply
#2
You use them like you use a function, the difference is that you can change them in realtime.

So a "function" might do one thing at the beginning of the code, and another thing later.

It's nice for optimizing, as you can make, for example a PSET routine that gets assigned based on BPP.

You have 3 different PSET routines, each for handling a diff. BPP depth.

You assign the correct BPP PSET routine to a function pointer, such as: my_pset

And you call it instead of somebpp_pset
Saving a few IF calls (IF BPP = X Then xBPP_Pset etc...)


Sorry, no example, anyone wanna whip one up?
Reply
#3
so, if you have a pointer to a function, you can just do the pointer?? like.... for instance

Code:
sub pointy

for p = 0 to 19

? p

next

end sub


dim r as pointy ptr

r = @pointy


then i could call it by just doing

Code:
r


? im confused

editted a couple things
Reply
#4
Yup, thats exactly how it works..

Though, you got the di wrong.. I tink..


But other than that, ya..

Code:
dim r as function ptr


Bah, wait for someone who has messed with it more than I have...
Reply
#5
^^ that was the biggest reason i didnt understand that pdf lib stuff... and win32 stuff... and.. Tongue

thanks
Reply
#6
sorry for double post this is what im trying now...


Code:
function yay As Integer

  For p = 0 To 9
    ? p
    
  Next
  
End Function

Screen 13

Dim u As Function() As Integer

u = @yay


Sleep

go = u
? go
Sleep
end

and i have no idea... >.<

edit: nvm just screwing around i figured it out <.< works if you do function as integer ptr and go = u()... thanks
Reply
#7
Function pointer are used mostly in callbacks, expecially when enumerating through the windows structures. Here is an example that enumerates all the windows on the desktop:

Code:
'Shows the use of a function pointer to implement a system callback
'to enumerate through all the windows on the desktop.
'Converted from a VB example.
'Note that in FreeBasic, Long and Integer are the same data type.
'=================================================================
'$include once: 'win\user32.bi'
'$Dynamic
option explicit

declare Function EnumWindowsProc(ByVal hWnd As Long, ByVal lParam As Long) As Long
declare Function GetAllWindows() As integer

dim shared WinHandle(1 to 1) As Long
dim shared WinHandleCount As Long
dim shared lWindows(1 to 1) As Long
dim Counter As Long
dim sWindows As String
dim WinTitle as string
dim TitleLen as long

WinHandleCount = 0  
If GetAllWindows() = True Then
   If WinHandleCount > 0 Then
      For Counter = 1 To WinHandleCount
         'Get the length of the buffer we need
         TitleLen = GetWindowTextLength(WinHandle(Counter)) + 1
         'Create the buffer
         WinTitle = space$(TitleLen + 1)
         GetWindowText(WinHandle(Counter), WinTitle, TitleLen + 1)
         WinTitle = trim$(WinTitle)
        print "hWnd: "; WinHandle(Counter), "Title : ";WinTitle
      Next
   End If
End If
sleep
end


'Here is the callback function that returns each window handle
Function EnumWindowsProc(ByVal hWnd As Long, ByVal lParam As Long) As Long
  'Inc the array index
  WinHandleCount = WinHandleCount + 1
  ReDim Preserve WinHandle(1 To WinHandleCount) As Long
  ' Add the information to the array
  WinHandle(WinHandleCount) = hWnd
  ' Tell the function to keep going
  EnumWindowsProc = 1
End Function

Function GetAllWindows() As Integer
  ' Start enumerating through the windows
  'The @ passes the address of EnumWindowsProc
  If EnumWindows(@EnumWindowsProc, 0) <> 0 Then
    GetAllWindows = True
  End If
End Function

I have only tried this on Win2K Pro.
Reply
#8
http://forum.qbasicnews.com/viewtopic.ph...ight=#1027
Antoni
Reply
#9
thanks antoni the first time it was way over my head but it makes a lot of sense now =) and thanks rel ;p hehe
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)