Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
FreeBasic OOP
#1
I am trying to get freebasic to do a bit of oop by defining some variables in a typeset as functions. However, I do not know how to give these functions access to the other variables in my datatype.. Here's the functionless code..

Code:
type myOBJ
setName as sub(xName as string)
getName as function() as string
name as string
end type

dim foo as myOBJ

declare sub setName(xName as string)
declare function getName() as string

'init
foo.setName = @setName
foo.getName = @getName


sub setName(xName as string)
end sub

function getName() as string

getName = "hello"

end function

while it's not hooked up, what I want to do is call setName, and assign the incoming value to the 'name' variable of the myOBJ datatype. Is this possible to do? Below is an example of how you would do it in java.


Code:
class Foo
  String name;
Foo() {
}
public void setName( String arg0) {
  this.name = new String();
  this.name = arg0;
}

Thanks!
Reply
#2
FB isn't oop oriented, but this should work:
Code:
type myOBJ
setName as sub(this as myOBJ ptr, xName as string)
getName as function(this as myOBJ ptr) as string
name as string
end type

dim foo as myOBJ

sub setName(this as myOBJ ptr, xName as string)
   this->name = xName
end sub
function getName(this as myOBJ ptr) as string
getName = this->Name
end function

'init
foo.setName = @setName
foo.getName = @getName

'test
foo.SetName (@foo, "Hello world!")
print foo.GetName (@foo)
sleep
url]http://fbide.sourceforge.net/[/url]
Reply
#3
Thanks for the help! Looks like I'm better off just creating functions and passing straight values.. that could get complicated hehe.
Reply
#4
nope, you just keep passing 'this' as an argument -c++ does that anyway thoo it is hidden from you.
url]http://fbide.sourceforge.net/[/url]
Reply
#5
I very much doubt c++ internally stores function pointers in each class instance though Wink
Reply
#6
it passes "this" as an optional first argument when you call functions -this is what I meant.
url]http://fbide.sourceforge.net/[/url]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)