function pointers

Previous topic - Next topic

Hemlos

Hmm i put proto and the struct at top, but all the lines are the same, and it will print 'Foo'.


Code (glbasic) Select
// --------------------------------- //
// Project: test-function-pointer
// Start: Wednesday, October 14, 2009
// IDE Version: 7.142


// SETCURRENTDIR("Media") // seperate media and binaries?

PROTOTYPE protoEchoName: self AS Foo


TYPE Foo
name$ = ""
echoNameMethod AS protoEchoName
ENDTYPE

LOCAL f AS Foo


f.echoNameMethod = echoName


// Main
f.name$ = "Foo"
f.echoNameMethod(f) // Line 16

SHOWSCREEN
KEYWAIT




FUNCTION echoName: self AS Foo
PRINT self.name$, 10, 10
ENDFUNCTION
Bing ChatGpt is pretty smart :O

Foo

Got it - thanks :D
Now I can rember - in C++, whats behind GLBasic, protoype functions must be first defined to use them.

Foo

#17
Another Problem - Pointers do not work in functions:
Code (glbasic) Select

// --------------------------------- //
// Project: test-function-pointer
// Start: Wednesday, October 14, 2009
// IDE Version: 7.142
LIMITFPS 10

   // SETCURRENTDIR("Media") // seperate media and binaries?

PROTOTYPE protoEchoName: self AS Foo


TYPE Foo
x = 0
y = 0
   name$ = ""
   echoNameMethod AS protoEchoName
ENDTYPE

LOCAL f AS Foo
LOCAL f2 AS Foo
LOCAL  list[] AS Foo

f.x = 10
f.y = 10


f2.x = 10
f2.y = 20
f2.name$ = "Foo 2"


f.echoNameMethod = echoName
f2.echoNameMethod = echoName

DIMPUSH list[], f
DIMPUSH list[], f2
ALIAS  ff AS list[0]
ALIAS ff2 AS list[1]


// Main
WHILE TRUE
PRINT "Test...", 10, 30
ff.name$ = "Foo " + RND(100)
echoAll(list[])

// Works
// FOREACH e IN list[]
// e.echoNameMethod(e) // Line 16
// NEXT
SHOWSCREEN
WEND

SHOWSCREEN
KEYWAIT

FUNCTION echoName: self AS Foo
//
self.echoName(self) // LINE 59 error : call to undefined function : Foo
   ///PRINT self.name$, self.x, self.y
ENDFUNCTION



FUNCTION echoAll: list[] AS Foo
FOREACH e IN list[]
e.echoNameMethod(e)
NEXT
ENDFUNCTION

Quote_______________________________________
*** Configuration: WIN32 ***
precompiling:
GPC - GLBasic Precompiler V.7.098 SN:46716f63 - 3D, NET
"test-function-pointer.gbas"(59) error : call to undefined function : Foo

Schranz0r

ohoh Gernot :/
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

codegit

#19
My hat goes off to Gernot for giving us function pointers. I think one of the problems is that everybody is going to want to use them in a different way. I would like to implement them similar to methods in objects. (Basic with a bit of OOP)  :whistle: Actually, if I remember correctly Schranz0r asked for exactly this way of using function pointers as well.  :good:

Like this...............
Code (glbasic) Select

PROTOTYPE Init: x, y, graphic

TYPE TPlayer
   x; y
   graphic
   ptr_init AS Init = InitPlayer
ENDTYPE
GLOBAL player AS TPlayer


// ------------------------------------------------------------- //
// ---  INITPLAYER  ---
// ------------------------------------------------------------- //
FUNCTION InitPlayer: px, py, pgraphic

   PRINT "x:" + px + "y:" + py + "Graphic:" + pgraphic, 0, 0

ENDFUNCTION // INITPLAYER

LOL..... Mr DRAWSPRITE  =D =D
------------------------------------------
1 X Acer TravelMate 4270, laptop, XP PRO
1 X Dell Studio 17 laptop, Windows 7
1 X MacBook Pro 2,2 GHz Core 2 Duo, 2 GB RAM, 160 GB HDD, 9400M
2 X iTouch
1 X HTC Desire (Android 2.1)
iPad soon to be added

Kitty Hello

Fix 1 - you're not calling the pointer!
Code (glbasic) Select

FUNCTION echoName: self AS Foo
   // self.echoName(self) // LINE 59 error : call to undefined function : Foo
   self.echoNameMethod(self)
ENDFUNCTION


Fix 2 - not possible. That's a fantasy of Schranz0r ;)
Code (glbasic) Select

   ptr_init AS Init // = InitPlayer


codegit


Fix 2 - not possible. That's a fantasy of Schranz0r ;)
Code (glbasic) Select

   ptr_init AS Init // = InitPlayer

[/quote]

:'( :'( :'( :'( :'( :'( :'( :'(
------------------------------------------
1 X Acer TravelMate 4270, laptop, XP PRO
1 X Dell Studio 17 laptop, Windows 7
1 X MacBook Pro 2,2 GHz Core 2 Duo, 2 GB RAM, 160 GB HDD, 9400M
2 X iTouch
1 X HTC Desire (Android 2.1)
iPad soon to be added

Schranz0r

Thats soooooooooo true  :'( :'( :'( :'( :'( :'( :'( :'(
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard