Posts: 3,288
Threads: 167
Joined: Nov 2001
It's for simulating "methods".
It's like OOP.
Instead of doing something like:
you can:
It's also a way to simplyfy object handling.
ie:
You can make AI's for each object using indexes(ids) rather than a buch of If then Else's or Seclect cases.
Posts: 68
Threads: 7
Joined: Dec 2004
relsoft, exactly.
OOP alike is what I'm looking for. For now, function will do most of needs.
it seem working with just calling it like OOP style:
[syntax="qbasic"]
defint a-z
option explicit
declare function add(i as integer, j as integer) as integer
dim pause as byte
type SOMETYPE
eax as integer
edx as integer
add as function(i as integer, j as integer) as integer
end type
dim shared reg as SOMETYPE
reg.add = @add()
reg.add(2, 4) '<-- executions
print
input "press ENTER to exit...",pause
function add(i as integer, j as integer) as integer
dim imm.eax as integer
reg.eax = i
reg.edx = j
imm.eax = i + j
print "reg.eax = ";reg.eax
print "reg.edx = ";reg.edx
print "reg.add(2, 4) => task + imm.eax"
print "imm.eax = ";imm.eax
add = imm.eax
end function
[/syntax]
= inc(¢)
Posts: 922
Threads: 15
Joined: Jun 2003
To make it OO-ish, that could to be:
Code:
declare FUNCTION add( byval this_ as SOMETYPE ptr, i AS INTEGER, j AS INTEGER) AS INTEGER
reg.add(@reg, 2, 4)
FUNCTION add( byval this_ as SOMETYPE ptr, i AS INTEGER, j AS INTEGER) AS INTEGER
DIM imm.eax AS INTEGER
this->eax = i
this->edx = j
imm.eax = i + j
PRINT ".eax = ";this->eax
PRINT ".edx = ";this->edx
PRINT ".add(2, 4) => task + imm.eax"
PRINT "imm.eax = ";imm.eax
add = imm.eax
END FUNCTION
With real classes the this_ argument would be passed automagically by the compiler, making it much simpler.. until there..
Posts: 68
Threads: 7
Joined: Dec 2004
I'm prefer this way if I had to use that '->' symbols, instead:
[syntax="qbasic"]
defint a-z
option explicit
declare function add(i as integer) as integer
dim pause as byte
type SOMETYPE
eax as integer
edx as integer
add as function(i as integer) as integer
end type
dim shared reg as SOMETYPE
reg.add = @add()
reg.eax = 2
reg.edx = 4
reg.add(reg.edx)
print
input "press ENTER to exit...",pause
function add(i as integer) as integer
dim this as SOMETYPE ptr
this = @reg.add-8
print "reg.eax = ";this->eax
print "reg.edx = ";this->edx
this->eax = this->eax + i
print "reg.add(reg.edx)"
print "reg.eax = ";this->eax
add = 0 'void
end function
[/syntax]
keep it more basic like with dot connection. don't like those '->' simbol for C like OOP. Btw, thanks for the ptr tips. May be, it will useful for DirectX header translations.
= inc(¢)
Posts: 251
Threads: 22
Joined: Dec 2004
Posts: 68
Threads: 7
Joined: Dec 2004
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(¢)