[solved]UDT and procptr

General FreeBASIC programming questions.
Post Reply
SamL
Posts: 58
Joined: Nov 23, 2019 17:30
Location: Minnesota

[solved]UDT and procptr

Post by SamL »

What am i doing wrong here?
im using FreeBASIC-1.10.0-winlibs-gcc-9.3.0

Code: Select all

type test_func_ptr
    
    declare function nex() as ulongint 
    declare function pre() as ulongint 
    declare function set_next() as ulongint
    declare function set_prev() as ulongint
    dim get_data as function() as ulongint

end type

function test_func_ptr.set_next() as ulongint
    this.get_data = procptr(this.nex)
    return 1
end function

function test_func_ptr.set_prev() as ulongint
    this.get_data = procptr(this.pre)
    return 1
end function

function test_func_ptr.nex() as ulongint
    print "next"
    return 1
end function

function test_func_ptr.pre() as ulongint 
    print "prev"
    return 1
end function


dim as test_func_ptr udt

udt.set_next() 'point get_data() to nex()
udt.get_data()

udt.set_prev() 'point get_data() to pre()
udt.get_data()
Last edited by SamL on Mar 02, 2024 19:12, edited 1 time in total.
fxm
Moderator
Posts: 12133
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: UDT and procptr

Post by fxm »

See the 5 corrected lines ('' *****):

Code: Select all

type test_func_ptr
    
    declare function nex() as ulongint 
    declare function pre() as ulongint 
    declare function set_next() as ulongint
    declare function set_prev() as ulongint
    dim get_data as function(Byref As test_func_ptr) as ulongint  '' *****

end type

function test_func_ptr.set_next() as ulongint
    this.get_data = procptr(test_func_ptr.nex)  '' *****
    return 1
end function

function test_func_ptr.set_prev() as ulongint
    this.get_data = procptr(test_func_ptr.pre)  '' *****
    return 1
end function

function test_func_ptr.nex() as ulongint
    print "next"
    return 1
end function

function test_func_ptr.pre() as ulongint 
    print "prev"
    return 1
end function


dim as test_func_ptr udt

udt.set_next() 'point get_data() to nex()
udt.get_data(udt)  '' *****

udt.set_prev() 'point get_data() to pre()
udt.get_data(udt)  '' *****

Extract of documentation of PROCPTR:
declare operator Procptr ( byref identifier as proctype [, any|user_proctype ] ) as internal_proctype ptr
.....
(internal_proctype has a supplementary first parameter byref as udt_name only for the member procedures of UDT udt_name)
SamL
Posts: 58
Joined: Nov 23, 2019 17:30
Location: Minnesota

Re: UDT and procptr

Post by SamL »

ok, that's interesting and what I needed.

Thanks fxm!
SamL
Posts: 58
Joined: Nov 23, 2019 17:30
Location: Minnesota

Re: [solved]UDT and procptr

Post by SamL »

I also figured out how to send parameters to the function.

Code: Select all

type test_func_ptr
    
    declare function nex(ttt as long) as ulongint 
    declare function pre(ttt as long) as ulongint 
    declare function set_next() as ulongint
    declare function set_prev() as ulongint
    dim get_data as function(Byref As test_func_ptr, ttt as long) as ulongint  '' *****

end type

function test_func_ptr.set_next() as ulongint
    this.get_data = procptr(test_func_ptr.nex)  '' *****
    return 1
end function

function test_func_ptr.set_prev() as ulongint
    this.get_data = procptr(test_func_ptr.pre)  '' *****
    return 1
end function

function test_func_ptr.nex(ttt as long) as ulongint
    print ttt
    print "next"
    return 1
end function

function test_func_ptr.pre(ttt as long) as ulongint 
    print ttt
    print "prev"
    return 1
end function

dim as test_func_ptr udt
dim as test_func_ptr udt2

udt.set_next()
udt2.set_prev()

udt2.get_data(udt2,4)
udt.get_data(udt,3)

sleep
Post Reply