Function vs Method on Types.
BlitzMax Forums/BlitzMax Beginners Area/Function vs Method on Types.
| ||
| Several concerns. In many instances I see inside custom types are created functions like methods, can understand this as well ?, I see you can use the keyword global for the variables. Type Car Global imageCar:TImage Field colorCar:String = "White" Function Run.... Method Gas... End Type |
| ||
My question is because in helping BlitzMax, for example in the class (type) Tlist, I see that there are functions and methods, this has me a little confused.MyList.Clear() ' Method. ClearList(ListaCar:TList) ' Function. |
| ||
| Functions in types are like static methods in C++, they don't have the implicit instance parameter (self in BlitzMax, this in C++). This means they can be called without constructing an instance first, however this also means they usually have no access to instance members. |
| ||
| Hi, if you look inside ClearList function you'll see... Function ClearList( list:TList ) list.Clear End Function...that it's using TList's Clear method inside. Functions are like globals in a sense that they exist in a static memory space and are created only once when you start your program, no mather how many objects are created with New operator. Methods and fields only exist when a new object is created and they are tide to that specific object instance, and are accessed through that objects reference created with New. -Henri |
| ||
Type TCar
Field color:string
Method Run()
print "run run run"
End Method
Function ConstructCar:TCar(color:string)
local c:TCar = new TCar
c.color = color
return c
End Function
End Type
'not possible
'TCar.Run()
'possible
TCar.ConstructCar("red").Run()
'most useful approach
local myCar:TCar = TCar.ConstructCar("red")
'now our car is a red one, ready to run
myCar.Run()
Short: - functions are "generic" and can be called from outside the type - methods need an instance of the type to get called but have access to its properties (print myCar.color) bye Ron |