Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
any function field types in udt example?
#16
Probably this will emulate inheritance (fake object extends):

[syntax="qbasic"]
defint a-z
option explicit


type REGISTER
eax as integer
edx as integer
add as function(i as integer) as integer
end type

type IMMEDIATE 'extending REGISTER class/object
reg as REGISTER
ebx as integer
mul as function(i as integer) as integer
end type

declare function add(i as integer) as integer
declare function mul(i as integer) as integer
'------------------------------------------------------

dim pause as byte
dim shared reg as REGISTER
dim shared imm as IMMEDIATE

reg.add = @add()
imm.reg.add = reg.add
imm.mul = @mul()
reg.eax = 2
reg.edx = 4
reg.add(reg.edx)
imm.reg = reg
imm.ebx = 2
imm.mul(imm.ebx)
print "-----------------------"
input "press ENTER to exit...",pause


'------------------------------------------------------
function add(i as integer) as integer
dim this as REGISTER ptr
dim that as IMMEDIATE ptr
dim root as string

if ( @i>=@reg and @i<=@reg.add ) then root="reg"
if ( @i>=@imm and @i<=@imm.mul ) then root="imm"

print "-----------------------"
if root="reg" then
this=@reg
print root+".eax = ";this->eax;" [";@this->eax;"]"
print root+".edx = ";this->edx;" [";@this->edx;"]"
this->eax = this->eax + i
print root+".add("+str$(i)+") <--[";@i;"]"
print root+".eax = ";this->eax
end if
if root="imm" then
that=@imm
print root+".eax = ";that->reg.eax;" [";@that->reg.eax;"]"
print root+".ebx = ";that->ebx;" [";@that->ebx;"]"
print root+".edx = ";that->reg.edx;" [";@that->reg.edx;"]"
that->reg.eax = that->reg.eax + i
print root+".reg.add("+str$(i)+") <--[";@i;"]"
print root+".eax = ";that->reg.eax
end if
add = 0 'void
end function

function mul(i as integer) as integer
dim this as REGISTER ptr
dim that as IMMEDIATE ptr
dim root as string

if ( @i>=@reg and @i<=@reg.add ) then root="reg"
if ( @i>=@imm and @i<=@imm.mul ) then root="imm"

print "-----------------------"
if root="reg" then
this=@reg
print root+".eax = ";this->eax;" [";@this->eax;"]"
print root+".edx = ";this->edx;" [";@this->edx;"]"
this->eax = this->eax * i
print root+".add("+str$(i)+") <--[";@i;"]"
print root+".eax = ";this->eax
end if
if root="imm" then
that=@imm
print root+".eax = ";that->reg.eax;" [";@that->reg.eax;"]"
print root+".ebx = ";that->ebx;" [";@that->ebx;"]"
print root+".edx = ";that->reg.edx;" [";@that->reg.edx;"]"
that->reg.eax = that->reg.eax * i
print root+".mul("+str$(i)+") <--[";@i;"]"
print root+".eax = ";that->reg.eax
end if
mul = 0 'void
end function
[/syntax]

But if you know how to write procedures which is could extends udt, it might help to have a working emulated OOP in FB. The best way is handle at compile time by the compiler.
= inc(¢) Big Grin
Reply


Messages In This Thread
any function field types in udt example? - by KiZ - 01-15-2005, 01:13 AM
any function field types in udt example? - by KiZ - 01-15-2005, 02:40 AM
any function field types in udt example? - by KiZ - 01-15-2005, 05:30 PM
any function field types in udt example? - by fsw - 01-15-2005, 11:16 PM
any function field types in udt example? - by zydon - 01-16-2005, 12:32 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)